4

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.

1

2 Answers 2

1

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.

1
  • That's the Perl rename command shipped by Debian and derivatives. It isn't present on Fedora, where rename is the utility from util-linux that's rarely powerful enough. Jan 9, 2015 at 22:13
0

Using the same rename command as @sputnick, combined with find:

find /path/to/collection -type f -execdir rename -n '$_ = lc;y/ /-/' {} +
  • $_ = lc converts the input to lowercase. y/ /-/ changes spaces to - (similar to tr).
  • -execdir runs the command in the file's directory, so that {} is replaced with ./name-of-file. Therefore directories should remain unaffected by it.
2
  • That's the Perl rename command shipped by Debian and derivatives. It isn't present on Fedora, where rename is the utility from util-linux that's rarely powerful enough. Jan 9, 2015 at 22:14
  • @Gilles I know (similar problem on Arch Linux, where it is called perl-rename, IIRC), which is why I co-opted sputnick's warning. (Even the linked manpage is for prename.)
    – muru
    Jan 9, 2015 at 22:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .