Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Google Music Beta won't upload mp3's with spaces in the actual filename. I would like a script that will find all the ones that have a space in the filename and copy them, rename them (just remove the spaces, or replace with underscore), and put them in a new directory so that it doesn't screw up my iTunes library. Thanks in advance! Also, i am running OS X.

share|improve this question

migrated from Jun 7 '11 at 17:51

This question came from our site for professional and enthusiast programmers.

In case you're seeking to do auto-imports, search for AppleScripts that add to iTunes library, eg – Denis Jun 7 '11 at 17:44

I don't remember if Perl comes installed on OS X by default, but if you have it, you can use this script to clean up your file names. Right now, it removes parentheses and apostrophes, and converts everything to lower case. You can add the line

$ARGV[$argnum] =~ s/ //g;

to remove spaces, or

$ARGV[$argnum] =~ s/ /_/g;

to convert spaces to underscores.

Save the script to your MP3 directory, then call the following from the command line:

./ *.mp3
share|improve this answer
okay, but I need the converted file to be put in a seperate directory. There are 150 of them buried in 17,000 mp3's – tomwolber Jun 7 '11 at 18:37
@tomwolber you can move the files first with: find . -type f -name '* *' -exec mv {} /dest/dir/ \; then run the cleanup – Rich Homolka Jun 7 '11 at 20:21
$ ls 
IAmAFile.mp3  I am a file with spaces in my name.mp3  IAmAnotherFile.mp3  I am another file with spaces in my name.mp3

Renaming them in a for loop...

$ for file in *mp3; do mv -i "$file" `echo "$file" | sed 's/ /_/g'`; done
mv: `IAmAFile.mp3' and `IAmAFile.mp3' are the same file
mv: `IAmAnotherFile.mp3' and `IAmAnotherFile.mp3' are the same file

... results in:

$ ls
IAmAFile.mp3  I_am_a_file_with_spaces_in_my_name.mp3  IAmAnotherFile.mp3  I_am_another_file_with_spaces_in_my_name.mp3

This works for all the files in your current working directory. It's a little ugly because mv gives an error for all the files that do not contain spaces, but it does it's job. The -i flag makes mv prompt before overwriting an existing fine, in case the underscore version already exists.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.