select folders (not files) with leading "_" (underscore) symbols in terminal

1.5k views Asked by At

I'm trying to run through folders and subfolders (only, no files can be altered) in a given directory which have leading underscores and remove those leading underscores. I'm planning on accomplishing this with a simple shell script:

for folder in ./_* do
  mv "$folder" "${folder:1}"
done

The above script doesn't work yet to specification for two reasons which I'm trying to correct here: - one, the "./_*" does not work like it should, either throwing an error (./_*: No such file or directory) or selecting folders which do not have leading underscores too. - two, it does not specify folders only...is there an option for the mv command which can do that?

Thanks

2

There are 2 answers

0
anubhava On BEST ANSWER

To find all folders starting with underscore use this find:

find . -type d -name '_*'

And to remove _ use:

find . -type d -name '_*' -exec bash -c 'f="$1"; mv "$f" "${f:1}" - {} \;
1
Gilles Quénot On

Using recursively :

shopt -s globstar

for dir in **/_*/; do
    mv "$dir" "${dir:1}"
done