简体   繁体   中英

Bash - Find several specific folders in a directory and its sbdirectories and rename these specific folders

I just started to study Bash. I want to do a script to find some specific folders in a directory and its subdirectories and if it exist, rename it into the same folder where we have found it. The same specific folder can be in some subdirectories.

I use this:

file=`find . -name a`
if [ -d $file ]
then
rename 's/a/b/' $file
fi

But don't work. Is there anyway to do this process?

Thanks.

Finally, i solved the problem with this:

find . -name "a" -type d -execdir rename 's/a/b/' {} \; &>/dev/null

You can do this with oneliner:

find . -name "a" -type d -execdir rename 's/a/b/' {} \\;

The parameter to name might be regex. With -type d it will find all directories. -execdir changes to a matching item's directory and then executes the rename command, passing the filename of the item at hand as an argument ( {} ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM