I need to recursively append the same suffix to all sub-directory names in a directory. What is a simple and safe way to do this?
1 Answer
Using find
and rename
:
find . -depth -type d -not -name '.' -exec rename -n 's/(.*)/$1_foo/' {} +
This will add suffix _foo
to all directories recursively, -n
will show the names of the directories that will be changed.
If you are satisfied with the names you can remove the -n
option to let the action take place:
find . -depth -type d -not -name '.' -exec rename 's/(.*)/$1_foo/' {} +
-
Why do you use
-not -name '.'
here? Will the current working directory be renamed without it?– kosJul 1, 2015 at 11:30 -
-
-
2
-
1no, your script have created
bar_foo
but there is a subfolder inbar
with the namefoo
Test your script with this structure:mkdir bar; mkdir bar/foo
– A.B.Jul 1, 2015 at 11:44