Give all files a .jpg extension

I have a directory and some of the files' extensions are in uppercase (.JPG) instead of lowercase.

I want to make sure every file has .jpg as its extension. How would I do this from a shell prompt?


Solution 1:

Using Shell Parameter Expansion:

for f in *.JPG; do
    mv "${f}" "${f%%.JPG}.jpg"
done

The " characters will take care of filenames containing spaces, as photographs often do.

Solution 2:

If you can use external tools (not only bash), check rename command!

rename .JPG .jpg *

The rename is part of util-linux.

Solution 3:

If you do have spaces in filenames:

for f in *.JPG; do [[ -f "${f}" ]] && mv "${f}" "${f/%JPG/jpg}"; done