Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Feb 8 '12 at 11:40

This question came from our site for professional and enthusiast programmers.

    
It's a shame that linux doesn't have AppleScript. –  gadgetmo Feb 13 '12 at 18:42
2  
Its so great that Linux has no AppleScript. –  math Feb 14 '12 at 20:54

5 Answers 5

if you don't have spaces in the filenames:

for f in *.JPG; do mv $f `echo $f | sed 's/JPG$/jpg/'`; done
share|improve this answer
1  
It'd be easy enough to make this work for files with spaces in the name: just add double quotes around both arguments to mv. –  Adam Zalcman Feb 8 '12 at 11:09
    
I don't have spaces. What do I add to make this happen in the CURRENT directory. At the moment I get error - mv: cannot stat `*.JPG': No such file or directory –  Mark Flint Feb 8 '12 at 11:26
1  
you don't have any JPGs there. –  Karoly Horvath Feb 8 '12 at 11:50

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.

share|improve this answer
5  
+1 This solution works even if you have spaces in the filename, but you mean "$f" instead of "f". –  Adam Zalcman Feb 8 '12 at 11:06
    
Shell Parameter Expansion is a bit cryptic, one could use basename instead: for f in *.JPG; do mv "$f" "`basename $f .JPG`.jpg"; done –  math Feb 14 '12 at 21:02
    
@math: I prefer my version as it works with filenames containing whitespace and will also work with /path/to/*.JPG. You're welcome to submit your own answer, of course :) –  Johnsyweb Feb 14 '12 at 21:17
    
Of course basename will strip of leading directories but will work with spaces inside filenames. (I don't think this is worth of an answer ;) so I posted it as a comment) –  math Feb 14 '12 at 21:25

If you do have spaces in filenames:

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

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

rename .JPG .jpg *

The rename is part of util-linux.

share|improve this answer
    
rename is... problematic. There's a version of rename that comes with perl that accepts different arguments to do the same thing. –  cha0site Feb 13 '12 at 11:04
    
Could you explain a bit more? Which version BTW? –  math Feb 14 '12 at 20:55

I would suggest to have a look at the mmv command. It's suited exactly for this kind of tasks. In your scenario it would be:

#> mmv "*.JPG" "#1.jpg"

The man page is full of examples, so it takes no time to get used to it.

On Debian systems it can be installed with:

#> aptitude install mmv
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.