How to change all file's extensions in a folder using one command on CLI in Linux?
5 Answers
Use rename:
rename 's/.old$/.new/' *.old
- 4,537
- 4
- 33
- 70
-
3The 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
- 39,348
- 9
- 57
- 69
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,021
- 1
- 29
- 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
- 395
- 2
- 11
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