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?


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/' '{}' +

If your version of bash supports the globstar option (version 4 or later):

shopt -s globstar
for f in **/*.t1; do
    mv "$f" "${f%.t1}.t2"
done 

None of the above solutions worked for me on a fresh install of debian 14. This should work on any Posix/MacOS

find ./ -depth -name "*.t1" -exec sh -c 'mv "$1" "${1%.t1}.t2"' _ {} \;

All credits to: https://askubuntu.com/questions/35922/how-do-i-change-extension-of-multiple-files-recursively-from-the-command-line


I would do this way in bash :

for i in $(ls *.t1); 
do
    mv "$i" "${i%.t1}.t2" 
done

EDIT : my mistake : it's not recursive, here is my way for recursive changing filename :

for i in $(find `pwd` -name "*.t1"); 
do 
    mv "$i" "${i%.t1}.t2"
done

Or you can simply install the mmv command and do:

mmv '*.t1' '#1.t2'

Here #1 is the first glob part i.e. the * in *.t1 .

Or in pure bash stuff, a simple way would be:

for f in *.t1; do
    mv "$f" "${f%.t1}.t2"
done

(i.e.: for can list files without the help of an external command such as ls or find)

HTH