Search code examples
regexlinuxbashshellsh

Recursively change file extensions in Bash


I want to recursively iterate through a directory and change the extension of all files of a certain extension, say .t1 to .t2. What is the bash command for doing this?


Solution

  • Use:

    find . -name "*.t1" -exec bash -c 'mv "$1" "${1%.t1}".t2' - '{}' +
    

    If you have rename available then use one of these:

    find . -name '*.t1' -exec rename .t1 .t2 {} +
    
    find . -name "*.t1" -exec rename 's/\.t1$/.t2/' '{}' +
    

    For a single file use the + delimiter and for renaming all files at once use the ; delimiter. Example: For a single file

    find . -name "*.t1" -exec bash -c 'mv "$1" "${1%.t1}".t2' - '{}' +
    

    And for all files in the scope of the find command:

    find . -name "*.t1" -exec bash -c 'mv "$1" "${1%.t1}".t2' - '{}' \;