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?
6 Answers
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' - '{}' \;
-
21
-
2(My version of
rename
doesn't allow the sed style substitution expression. Gotta love Linux. I used to have to install TotalCommander for Windows to do stuff like this.) Commented Dec 9, 2015 at 0:03 -
7In case anyone is wondering what the
"${1%.t1}".t2
part does, like I did: It uses bash string manipulation to do the following: 1/ Take the first positional parameter$1
and truncate the.t1
string literal from its end (percentage sign%
operator). 2/ Append the.t2
string literal to the result.– ZackCommented Jul 21, 2017 at 1:55 -
5prefer to user
find . -type f -name '*.t1'
to avoid folders Commented Jan 15, 2019 at 11:44 -
17
None of the suggested solutions worked for me on a fresh install of debian 11. 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
-
2Thanks for this. The rename stuff in the selected answer doesn't work on Ubuntu 20 Commented Nov 6, 2022 at 21:17
-
This is the only one worked for me. None of the accepted answer methods worked. I am on Ubuntu so probably a Debian/CentOs whatever thing. cheers– NeoCommented Nov 17, 2022 at 19:35
-
1
-
1ty, this worked great. It would be great to include a short explanation for some of the pieces of the command that aren't intuitive to beginners (ex.
_ {} \;
)– aspen100Commented Dec 14, 2023 at 16:43
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
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
-
10Don't parse ls, and see the same page for why your
find
syntax is bad. Also, make sure you quote your variables Commented Feb 24, 2014 at 12:26
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
-
8I assume the OP's use of "recursively" refers to renaming files in subdirectories of the directory as well.– chepnerCommented Feb 27, 2014 at 0:59
-
1
My lazy copy-pasting of one of these solutions didn't work, but I already had fd-find
installed, so I used that:
fd --extension t1 --exec mv {} {.}.t2
From fd
's manpage, when executing a command (using --exec
):
The following placeholders are substituted by a
path derived from the current search result:
{} path
{/} basename
{//} parent directory
{.} path without file extension
{/.} basename without file extension
rename
, write a shell script, which renames a single file (trivial to do in your simple case), and then usefind
to apply this script to all files with the offending extension.