How to change all file's extensions in a folder using one command on CLI in Linux?
6 Answers
Use rename
:
rename 's/.old$/.new/' *.old

- 4,871
- 4
- 38
- 73
-
6The above did not work for me. But this worked for me. >>rename .htm .html *.htm – viswas Mar 30 '16 at 07:39
If you have the perl rename
installed (there are different rename
implementations) you can do something like this:
$ ls -1
test1.foo
test2.foo
test3.foo
$ rename 's/\.foo$/.bar/' *.foo
$ ls -1
test1.bar
test2.bar
test3.bar

- 42,970
- 10
- 60
- 71
You could use a for-loop on the command line:
for foo in *.old; do mv $foo `basename $foo .old`.new; done
this will take all files with extension .old and rename them to .new

- 4,181
- 1
- 30
- 39
This should works on current directory AND sub-directories. It will rename all .oldExtension files under directory structure with a new extension.
for f in `find . -iname '*.oldExtension' -type f -print`;do mv "$f" ${f%.oldExtension}.newExtension; done

- 475
- 5
- 11
-
The second arg should be wrapped with `"` (cannot edit: "too many pending edits" issue). – Daniel-KM Jul 25 '23 at 07:51
This will work recursively, and with files containing spaces.
Be sure to replace .old
and .new
with the proper extensions before running.
find . -iname '*.old' -type f -exec bash -c 'mv "$0" "${0%.old}.new"' {} \;

- 155
- 8
Source : recursively add file extension to all files (Not my answer.)
find . -type f -exec mv '{}' '{}'.jpg \;
Explanation: this recursively finds all files (-type f) starting from the current directory (.) and applies the move command (mv) to each of them. Note also the quotes around {}, so that filenames with spaces (and even newlines...) are properly handled.
-
Actually this does not remove the old extension, but rather appends the new extension. – MichielB May 30 '14 at 09:00