This question already has an answer here:
-
How do I rename the extension for a batch of files?
18 answers
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?
If you have rename available then use:
find . -name "*.t1" -exec rename 's/\.t1$/.t2/' '{}' \;
If rename isn't available then use:
find . -name "*.t1" -exec bash -c 'mv "$1" "${1%.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
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" "${i%.t1}.t2"
done
(i.e.: for
can list files without the help of an external command such as ls
or find
)
HTH
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