Marc Fearby's Home Page

Bulk renaming files in Linux

For some time it’s been annoying me that the various GUI file renaming options in Linux weren’t quite good enough. They either require manual previewing/actioning each and every directory or those that do recursive processing were just very limited.

So, after finding this offering to get me started, enter the following shell script which I’ve put together to replace the three most annoying characters that Linux allows in file names (and which the various MP3 ripping programs also permit and don’t clean up):

#!/bin/bash

# Replace double quotes with single quotes
find . -name '*"*' -depth -exec rename -v "s/\"/\'/g" {} +

# Strip question marks entirely
find . -name '*\?*' -depth -exec rename -v "s/\?//g" {} +

# Replace colons with periods
find . -name '*:*' -depth -exec rename -v "s/\:/\./g" {} +

Save the above to a file and set it to executable (chmod 755 filename.sh) and Bob’s your uncle. I still have quite a lot of tidying and tagging ahead of me after going on a CD-buying binge for the past 12 months, thanks to a stellar Aussie Dollar exchange rate, but at least I can now copy various MP3 rips to a FAT32 USB key and take them to work without Windows Explorer bitching at me.

Meh. The code isn’t showing entirely in the correct font/colour but I’m sure you’ll cope :-)

Leave a Reply