2011/08/09

The rename command

I bought a new home NAS drive and started copying the bunch of pictures in my computer to it. So I faced the problem that either nautilus, SMB or the network drive's filesystem couldn't manage file names having special shell characters.

I happened to have some old picture files that I initially stored in a Windows machine and later moved to my current linux system. The pictures were taken during my semester abroad in Copenhagen, and I found it nice to use the danish name of Copenhagen for the picture file names. So the names of these files initially looked like

København-21042003 <three digits>.jpg

and after moving them into my linux ext3 drive they looked like

K?benhavn-21042003 <three digits>.jpg

which has an interrogation mark ('?') instead of the danish letter 'ø'.

Besides the fact that this kind of file names becomes a shell issue, it's been a while since I decided to avoid file names having spaces and/or letter that are not in the english alfabet, e.g. my beloved 'ñ'. It's a simple rule which is not a pain to follow and just makes life easier.

So I could manually rename all the files one at a time using the nautilus interface (right click menu). But considering that the amount of files raised up to more than 50, I thought it might be easier to use the shell.

I remembered that a while ago, at work, I had to write a script that copied some files renaming them performing a substitution of a pattern in the file names like

literalfoo.ext -> literalbar.ext

and I used a variable to store the file name, then edited its value using sed, and then actually copying the files. So my first thought was to do the same moving instead of copying. But I suspected there should be a tool for easy multiple file renaming and I googled it.

So that's how I found this article in the Debian Adminstration Site and learned about the rename command. So I just had to use this command line

$ rename 's/K.*-(.*\.jpg)/Koebenhavn-\1/' *.jpg

to turn all the file names into

Koebenhavn-21042003 <three digits>.jpg

And the best thing about it was the '-n' option that performs a dry run that just prints out what the command would do, so it lets you try and refine your regular expressions if you are not too familiar with them (as is my case).