July 07, 2008

How-To: Mass-Rename Files Using an Easy Method

You may find this useful to rename MP3 or image files in a directory, so they will all look the same.

The following script will work on directories with less than 100 files of the same type, and will rename all of them to file01, file02, file03, ... and so on.
j=1; 
for i in *; do

if [[ $j -lt 10 ]]; then
mv "${i}" "file0${j}";
fi;
if [[ $j -ge 10 && $j -lt 100 ]]; then
mv "${i}" "file${j}";
fi;
j=$(($j+1));
done
You can only use it for certain types of files in a directory, like this:
j=1;
for i in *.ogg; do
if [[ $j -lt 10 ]]; then
mv "${i}" "audio0${j}.ogg";
fi;
if [[ $j -ge 10 && $j -lt 100 ]]; then
mv "${i}" "audio${j}.ogg";
fi;
j=$(($j+1));
done
This will rename all the Ogg files in a directory to audio01.ogg, audio02.ogg, audio03.ogg, and so on. You can test it for whatever files you like, however make a back-up first, since you can accidentally rename files or even directories which you don't want renamed.

Updated: Jul 07, 2008 (Created: Jul 07, 2008)

4 comments:

Anonymous said...

mmv does the job in one line

Anonymous said...

Also, have a look at "seq -w 1 100".
Jaap

Ihar Filipau said...

man rename

beauty is that it is present on many GNU and/or Linux systems already as part of util-linux package.

Anonymous said...

krename as a gui does a good job

http://www.krename.net