Batch File Extension Renaming – Bash Command Line Scripting

How many times have you had a bunch of files that you wanted to change the extension on? And all you really want to do is make mv work differently:

mv *.jpeg *.jpg

Well, if you’re on a Linux, Unix, or Mac box, you’re in luck – this is easy to achieve. All you need to do is list all of the files you’re looking for:

ls ./*.jpeg

…pipe that through sed, and apply a regular expression replacement (remove the jpeg):

ls ./*.jpeg | sed -e 's/.jpeg//'

…and then use xargs to get the output of sed, store it in an expression, {}, and pass it to mv:

ls ./*.jpeg | sed -e 's/.jpeg//' | xargs -t -i {} mv {}.jpeg {}.jpg

That should work on most boxes. Macs might freak and think that xargs -t -i {} should be xargs -t -I {}.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>