How to rename thousands of files efficiently

filenamesfilesrename

I have a bunch of music files (literally thousands of files) in my Fedora PC that I'd like to rename with two rules:

  1. Every space must be replaced by the character _ (underscore), and
  2. Every letter in upper case must be replaced by its lower case counterpart.

So for example, the filename of the song Under A Glass Moon, which is the sixth track on the album, is named 06 Under A Glass Moon.mp3 in my computer and I would like it to be named 06_under_a_glass_moon.mp3.

My question is: Is it possible to do this to every song/file in my collection without having to do it manually.

Note: Just to clarify, I don't need to edit the metadata of the songs; I do that with EasyTAG. All I need to do is to modify the song's filenames.


Update: I know this question has some time and some people have already answered, but I found a simpler solution using EasyTAG. The way to do this is going:

View --> Scanner Mode --> Process Fields. 

There you can choose to replace certain character for others (not limited to replacing " " by "_"), you can choose to lowercase or uppercase at will, and many other things. Moreover, you can decide to which fields you wish to make the changes. Very useful feature.

Also, thanks to those who answered. Your help has been very useful.

Best Answer

You can install the Perl script rename. Then try doing this :

$ rename -n 's/[A-Z]/lc($&)/ge; s/\s/_/g' files*

(remove the -n switch when your tests are OK)


There are two utilities called rename. The one in Fedora can't do this. Some other distributions come with the Perl one by default. If you run the following command (GNU)

$ file "$(readlink -f "$(type -p rename)")"

and you have a result like

.../rename: Perl script, ASCII text executable

and not containing:

ELF

then this seems to be the right tool =)

If not, such as on Fedora, install it manually.

Last but not least, this tool was originally written by Larry Wall, Perl's dad.

Related Question