4

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?

3

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/' {} +
8
  • Why do you use -not -name '.' here? Will the current working directory be renamed without it?
    – kos
    Jul 1 '15 at 11:30
  • ls bar_foo/ gives foo
    – A.B.
    Jul 1 '15 at 11:31
  • Can't rename ./bar/foo ./bar/foo_foo:
    – A.B.
    Jul 1 '15 at 11:32
  • 2
    You're indeed right, it's being listed. Well upvoted it already
    – kos
    Jul 1 '15 at 11:34
  • 1
    no, your script have created bar_foo but there is a subfolder in bar with the name foo Test your script with this structure: mkdir bar; mkdir bar/foo
    – A.B.
    Jul 1 '15 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.