PDA

View Full Version : A script to change the extension of multiple files


akubra
2007-01-26, 12:02 AM CST
I spent way longer than I should have searching for an easy script to rename the extensions of a group of files in a folder. I found a few that were either overcomplicated or just plain didn't work, especially on filenames that contain spaces!

Here is the simple script that I ended up writing to change a couple of .bat files to .text files:

#!/bin/bash
for f in *.dat;
do
tempfile="`basename "$f" .dat`.text";
echo $tempfile;
mv "$f" "$tempfile";
done;

I saved this sript as ext_changer in the directory containing the .dat files I wanted changed to .text files, then:
$ chmod +x ext_changer
$ ./ext_changer
And the extensions are changed!

You can easily change this script to convert any script to any other script, just change all the .dat 's to whatever you're changing from and all the .text 's to whatever you want to change to. Actually, if you're a fan of the quick lazy way, try this:
$ sed -e 's/.bat/.jpg/' < ext_changer > jpg_changer
This changes all the occurrences of ".bat" to ".jpg" and saves the changed script to "jpg_changer"