Unix command in terminal to replace certain characters in files

Discussion in 'macOS' started by djrobsd, Jun 28, 2009.

  1. djrobsd macrumors 6502a

    Joined:
    May 2, 2008
    #1
    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! :)
     
  2. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #2
    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 :)
     
  3. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
    Terminal is fun and all, but you should also take a look at Name Mangler.
     
  4. jc1350 macrumors 6502a

    Joined:
    Feb 4, 2008
    #4
    I second name mangler. Great program!
     
  5. djrobsd thread starter macrumors 6502a

    Joined:
    May 2, 2008
    #5
    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. :)
     
  6. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #6
    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.
     
  7. MacUser2525 macrumors 68000

    MacUser2525

    Joined:
    Mar 17, 2007
    Location:
    Canada
    #7
    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.
     
  8. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #8
    Automator's "Rename Finder Items" has a "Replace Text" option that should do the trick.

    mt
     
  9. Ti_Poussin macrumors regular

    Ti_Poussin

    Joined:
    May 6, 2005
    #9
    This is a perfect job for automator, I guess Apple don't push enough on this and smart folder
     
  10. Ti_Poussin macrumors regular

    Ti_Poussin

    Joined:
    May 6, 2005
    #10
    Here, I did it in few secondes :D
     

    Attached Files:

Share This Page