Advanced

Mass-Rename Files Using an Easy CLI Method

Posted by Chris7mas on · Post a comment
Echoes; By Craciun Dan
Mail this story
Print this story

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.

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.

Full Story

  Nav
» Read more about: Story Type: Tutorial; Groups: Linux

» Get new stories by e-mail: nightly or as they are published

« Return to the newswire homepage

  Related stories
» More like this

Comments

No comments yet. Be the first.

Post a comment

A free account is needed to post. Write your comment here first: your text is kept in this browser while you sign up or log in, and is waiting for you when you come back.

Your message:
|||||
10
Markdown: **bold**, *italic*, [text](https://link), - lists, > quotes, $x^2$ for math. Formatting help

Sign up and post (it is free)  or  Log in and post