Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip leading dot from filenames bash script

Tags:

linux

find

bash

I have some files in a bunch of directories that have a leading dot and thus are hidden. I would like to revert that and strip the leading dot.

I was unsuccessful with the following:

for file in `find files/ -type f`;
do
base=`basename $file`
if [ `$base | cut -c1-2` = "." ];
then newname=`$base | cut -c2-`;
dirs=`dirname $file`;
echo $dirs/$newname;
fi
done

Which fails on the condition statement:

[: =: unary operator expected

Furthermore, some files have a space in them and file returns them split.

Any help would be appreciated.

like image 397
Radek Avatar asked Jan 21 '11 19:01

Radek


People also ask

What does $() mean in bash?

Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).

What does [[ ]] mean in bash?

The [[ ... ]] part allows to test a condition using operators. Think of it as an if statement. In your example, you're using the -s operator, which tests that the referenced file is not empty.

What is DOT in bash script?

(dot) runs a shell script in the current environment and then returns. Normally, the shell runs a command file in a child shell so that changes to the environment by such commands as cd, set, and trap are local to the command file.

How do I shorten a string in bash?

There is a built-in function named trim() for trimming in many standard programming languages. Bash has no built-in function to trim string data. But many options are available in bash to remove unwanted characters from string data, such as parameter expansion, sed, awk, xargs, etc.

When to use the dot command in Bash?

When to use the Dot Command in Bash? First of, the dot command (.) is not to be confused with a dot file or a relative path notation. For example, the ~/.ssh folder is a hidden folder using the dot file notation, you will need to use ls -a to see that folder.

How to remove leading and trailing space or character from string data?

`sed` command is another option to remove leading and trailing space or character from the string data. The following commands will remove the spaces from the variable, $myVar using `sed` command.

How do I trim a string in Bash?

Sometimes it requires to remove characters from the starting and end of the string data which is called trimming. There is a built-in function named trim () for trimming in many standard programming languages. Bash has no built-in function to trim string data.

How to remove unwanted characters from string data in Bash?

Bash has no built-in function to trim string data. But many options are available in bash to remove unwanted characters from string data, such as parameter expansion, sed, awk, xargs, etc.


1 Answers

The easiest way to delete something from the start of a variable is to use ${var#pattern}.

$ FILENAME=.bashrc;    echo "${FILENAME#.}"
bashrc
$ FILENAME=/etc/fstab; echo "${FILENAME#.}"
/etc/fstab

See the bash man page:

${parameter#word}
${parameter##word}

The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘‘#’’ case) or the longest matching pattern (the ‘‘##’’ case) deleted.

By the way, with a more selective find command you don't need to do all the hard work. You can have find only match files with a leading dot:

find files/ -type f -name '.*'

Throwing that all together, then:

find files/ -type f -name '.*' -printf '%P\0' |
    while read -d $'\0' path; do
        dir=$(dirname "$path")
        file=$(basename "$path")
        mv "$dir/$file" "$dir/${file#.}"
    done

Additional notes:

  1. To handle file names with spaces properly you need to quote variable names when you reference them. Write "$file" instead of just $file.

  2. For extra robustness the -printf '\0' and read -d $'\0' use NUL characters as delimiters so even file names with embedded newlines '\n' will work.

like image 175
John Kugelman Avatar answered Sep 23 '22 14:09

John Kugelman