12

How to change all file's extensions in a folder using one command on CLI in Linux?

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222

6 Answers6

18

Use rename:

rename 's/.old$/.new/' *.old

DBedrenko
  • 4,871
  • 4
  • 38
  • 73
5

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
Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
4

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

MichielB
  • 4,181
  • 1
  • 30
  • 39
1

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
Mhar Daniel
  • 475
  • 5
  • 11
0

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"' {} \;

-2

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.

Community
  • 1
  • 1
NooJ
  • 85
  • 3
  • 10