Rename files and directories recursively under ubuntu /bash

后端 未结 8 1164
执念已碎
执念已碎 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:25

    @speakr's answer was the clue for me.

    If using -execdir to transform both files and directories, you'll also want to remove -type f from the example shown. To spell it out, use:

    find /your/target/path/ -execdir rename 's/special/regular/' '{}' \+

    Also, consider adding g (global) flag to the regex if you want to replace all occurrences of special with regular in a given filename and not just the first occurrence. For example:

    find /your/target/path/ -execdir rename 's/special/regular/g' '{}' \+

    will transform special-special.jpg to regular-regular.jpg. Without the global flag, you'll end up with regular-special.jpg.

    FYI: GNU Rename is not installed by default on Mac OSX. If you are using the Homebrew package manager, brew install rename will remedy this.

提交回复
热议问题