Rename files and directories recursively under ubuntu /bash

后端 未结 8 1165
执念已碎
执念已碎 2020-11-29 18:50

I want to rename all files and directories that contain the word \"special\" to \"regular\". It should maintain case sensitivity so \"Special\" won\'t become \"regular\".

8条回答
  •  星月不相逢
    2020-11-29 19:38

    For those just wanting to rename directories you can use this command:

    find /your/target/path/ -type d -execdir rename 's/special/regular/' '{}' \;
    

    Note type is now d for directory, and using -execdir.

    I haven't been able to work out how to rename both files and directories in a single pass though.

    Someone commented earlier that once it renamed the root folder then it couldn't traverse the file tree any more. There is a -d switch available that does a depth traversal from the bottom-up, so the root would be renamed last I believe:

    find -d /your/target/path/ -type d -execdir rename 's/special/regular/' '{}' \;
    

    From the manpage (man find):

     -d      Cause find to perform a depth-first traversal, i.e., directories are visited in post-order and all entries in a directory will be
             acted on before the directory itself.  By default, find visits directories in pre-order, i.e., before their contents.  Note, the
             default is not a breadth-first traversal.
    

提交回复
热议问题