Thursday 7 August 2014

Linux Ubuntu Mint - Get Rid of Double Extensions in File Names

One of the fun things about Linux is that you can do almost anything easily and quickly by writing short little programs that do things in minutes that would otherwise take hours.

I often edit digital photographs.  This usually involves opening GIMP (like Photoshop - but FREE) and applying various changes, one image at a time.

But what happens when you have a hundred images and you want to make the same changes to each one?  That's where Linux shines.  You write a little text file of instructions, sometimes only a few lines long, save it, set it so it is executable (a couple of clicks) and you have a program.

Unfortunately sometimes I forget the right way to do things.  I've even used commercial programs and had this problem.  I set things up so I start with a .jpg file but during the conversion I am supposed to end up with a .png image.  And I do.

 But the file is called something like my_image.jpg.png

So how to get rid of that pesky .jpg in the name, left over from the original file name?

This works (in Linux) and something similar probably works in some other operating systems.  Since I work in Linux, it will do for me.

for i in *.png
do
    mv "$i" "`echo $i | sed 's/.jpg//'`"
done


NOTE!  By changing it a little, saving it as a text file and marking it executable (in right click > properties > Permissions)  you can easily use it any time you want.  Just copy it to a folder full of files with double extensions (somefile.jpg.png) and click it.

#!/bin/bash
# get rid of the double extension .jpg.png
for i in *.png
do
    mv "$i" "`echo $i | sed 's/.jpg//'`"
done



No comments:

Post a Comment