Batch changing file extensions

December 24, 2007

This is a simple command that will change the file extentions of all files in a directory. It is rather simple and all it requires is base, for, do, mv, and echo. All of which any Unix based operating system should have. This also works on OS X.

For this example I used bash on Debian 3.1. I had a directory full of JPEG images that had an uppercase extension of JPG. Uppercase extensions are a pet peeve of mine. Not sure why, but I cannot stand them. I think maybe because I am a web developer so I try to keep everything lowercase for ease of use. Anywho…I had a directory full of files with the uppercase JPG extension and I wanted them all converted to lowercase jpg. Here is the code I used.

for file in * ; do mv "$file" `echo $file | sed s/JPG/jpg/g` ; done

Any file having JPG in it will have JPG converted to jpg. Simple, quick, done. If you wish to convert something else simply change s/JPG/jpg/g to something else. For example, changing all doc extensions to txt should work like so:

for file in * ; do mv "$file" `echo $file | sed s/doc/txt/g` ; done

I hope that helps someone out there. I found it useful enough to share with ya’ll!

One thought on “Batch changing file extensions

  1. Lanai (October 28, 2008)

    Great work.