How can I change all files prefixes in one command?

I need to change the postfixes of all files (all the same .JPEG) to .jpeg (Capital vs. lower case).
Is there a quick way of doing so?


Solution 1:

Use the Perl program rename which is installed by default:

rename 's/\.JPEG$/.jpeg/' *.JPEG

The first argument is a Perl regular expression matching filenames ending with .JPEG and replaces it with .jpeg.

The second argument selects the files that should be matched, in your case every file in the current directory ending on .JPEG. You could specify a different location of course:

rename 's/\.JPEG$/.jpeg/' ~/Pictures/*.JPEG

Other answers I've seen:

  • rename s/.JPEG$/.jpeg/ * - this will also rename files like StupidJPEG to Stupi.jpeg because the dot is matches any character. .JPEG$ is a regular expression
  • rename 's/\.JPEG$/\.jpeg/' * - works, but it's less efficient because it passes all files in the current directory to rename.
  • rename -n 's/.JPEG$/.jpeg/' *.JPEG - the -n option would show the files being renamed, without actually renaming them ("dry run"). Because *.JPEG matches files postfixed with .JPEG only, the dot-matches-all issue is non-existent here.

Solution 2:

Although this is possibly not the best solution for this particular usage case,

for i in *.JPEG; do mv "$i" "$(basename "$i" .JPEG).jpeg"; done

also works. We can do some trickyness with bash in order to slightly increase efficiency (avoiding in invocation of an additional sub-process in the inner loop), ending up with:

for i in *.JPEG; do mv "$i" "${i%%.JPEG}.jpeg"; done

This solution is most useful if you want to do something else in additon to renaming the files, such as logging what names were changed, or even just doing a dry run to ensure that it does what you want.

Solution 3:

There is a tool for this:

sudo apt-get install renameutils or click renameutils

(if not already installed)

where you can do (from command line):

rename s/\.JPEG$/\.jpeg/ *.JPEG

Solution 4:

Found it a second after posting:

rename 's/\.JPEG$/.jpeg/' *