Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

djrobsd

macrumors 6502a
Original poster
Hi,

I have a boat load of mp3 files that have a _ instead of a space in the file name. I figure there must be a magical command in the Unix terminal window that would rename the file based on a replacement string.

I want to replace all _ in a file name with a Space. Is there an easy way to do this? Or, maybe a free utility I can download? I'm trying to clean up my files! 🙂
 
There most certainly is an "easy" way to do this from within terminal. The following will work, but it assumes all of your mp3 files are in the directory in which you are running this command from. It is not much harder to make this more generic, so it will find all mp3 files in the current directory and any subdirectories, but here you go ...

Code:
$ for i in *.mp3; do mv "$i" "`echo $i | sed -e 's,_,\ ,g'`"; done

Make sure you get the single quotes, double quotes and backticks right, or else it won't work. In the sed command, there is a single space after the backslash (\).

BTW, if you want to know what this is doing:

The part up until the first ; means to do a for loop on all mp3 files. Within the for loop (the part between the first ; and "done"), the variable $i represents an individual mp3 file. Then there is a move command (mv) that will rename an individual file to the same name, but replacing the _ with a space.

The "`echo $i | sed -e 's,_,\ ,g'`" part represents the new file name. It is echoing the current name of the file and piping it to 'sed'. Sed is a great line editor that is widely used in batch scripts and what not to do things like this. The backticks (`) mean "do this in another shell and return the answer". The "g" in the sed command means "do the substitution for all _ you find in the filename instead of just the first one." The backsplash is an escape for the space. Spaces in filenames, in the unix world, can be quite evil and cause problems because you always need to escape the spaces or quote the filename.

If you want to know more about what any of these commands do, "man" is your friend ($ man sed).

Edit: I did test this, but you might want to test it out first with a few files in their own directory. There is no "undo" command in the command line world 🙂
 
WOW! That is awesome, thank you everyone for your wonderful suggestions!!!! Now if I could get Thunderbird with Attachment Extractor to automatically download all new attachments then I could script all this out and automate my daily email files routine. 😉

That, and automatically moving all files that are truncated MS DOS file names, the ones that have the ~ sign in them, like donhe~1.mp3 into their own folder and then find an ID3 renamer utility... To rename those files with their ID3 tag. 🙂
 
That, and automatically moving all files that are truncated MS DOS file names, the ones that have the ~ sign in them, like donhe~1.mp3 into their own folder and then find an ID3 renamer utility... To rename those files with their ID3 tag. 🙂

You can do something similar to what I posted above to move the files:

Code:
$ mkdir ~/temp
$ for i in `find ~/ -name "*~?.mp3"`; do mv "$i" ~/temp; done

That will move all files found in your home directory and any subdirectories. Might want to just do find ~/ -name "*~?.mp3" first just to make sure it is grabbing all the files you want.

Can't help you with the ID3 tag thing (unless you know of a command line ID3 tag program that will pull out the name so you can use it in a bash script 🙂 ). There must be a GUI programs that can help with that.
 
then find an ID3 renamer utility... To rename those files with their ID3 tag. 🙂

Jaikoz and look at creating a droplet with Name Mangler to just be able to drag and drop the files you want renamed without opening the program itself to do it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.