Recursively change file extensions in Bash

Amal Antony picture Amal Antony · Feb 24, 2014 · Viewed 54.3k times · Source

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?

Answer

anubhava picture anubhava · Feb 24, 2014

If you have rename available then use:

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

or else:

find . -name '*.t1' -exec rename .t1 .t2 {} +

If rename isn't available then use:

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