Replacing underscores with slashes in filenames

Discussion in 'Windows, Linux & Others on the Mac' started by haravikk, Mar 4, 2013.

  1. haravikk macrumors 65816

    Joined:
    May 1, 2005
    #1
    Okay, so for ages I've been saving files with FireFox not realising that it was making the same mistake that most programs do (including several Apple ones) and replacing forward slashes in filenames with underscores or some other symbol that I don't want.

    Anyway, this means I've saved a ton of files into a folder that have underscores where they should have forward slashes that, aside from simply being wrong, breaks consistency with all the files in there that are actually correct. For those wondering why it matters; I've been saving some files with "tags" in the name in the format:
    Code:
    MyFile [tag A/tag B/tag C]
    So that I can process them easily by script later on. While I could have my script watch out for this, I'm not the only one producing files like this and so and underscore is technically valid within a tag, so I want to get rid of them now.


    So what I'm looking for is a unix command for finding invalid file-names and swap the underscores for slashes. So I've come up with the following from various guides from a search:
    Code:
    find . -name '*\[*_*\]*'|while read -r file
    do
     mv "'$file'" "${file//[_]/\/}"
    done
    However while the find statement on its own correctly lists all the broken file-names, the mv command complains of "no such file or directory" on all files.

    I'm assuming it's a problem with how I'm inserting my forward slashes; should I be using AppleScript instead for this since it interprets file-paths differently (using colons for directory separation such that slashes are fine within individual directory/file names)?
     
  2. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #2
    You can't do this because the slash is an invalid character for file names as it is used as the path separator.

    Pick any other character and you will be fine.
     
  3. haravikk thread starter macrumors 65816

    Joined:
    May 1, 2005
    #3
    Not on HFS+ it's not; try it in the Finder, you can add slashes as much as you like. As I say, I have tons of files with slashes already in them as I wanted, the problem is files saved by FireFox and some other programs which aren't aware that it's okay to save files and directories with slashes in them.
     
  4. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #4
    Finder tries to be compatible with older Mac OSes rather than UNIX. When you use a slash in Finder it gets converted to a colon in the file system. You can't use a colon in Finder because it wasn't allowed in older Mac OSes, but you can from the command line, and it looks like a slash in Finder.

    So if you simply want the files to appear like they have slashes in them, just use colons instead at the command line. But to avoid confusion you really should use another character.
     
  5. printz macrumors regular

    Joined:
    Dec 23, 2012
    #5
    Interestingly, Windows also forbids you from putting colons in names; I wonder why. Because they're used to name URL protocols?
     
  6. haravikk thread starter macrumors 65816

    Joined:
    May 1, 2005
    #6
    Ah, thanks that makes sense then; I'll just swap in colons on the "broken" file-names for now, but I'll see about finding something different. Shame though as slashes suit just fine, but I can't use commas as I also have tag groups so I'm already using those, bah.

    What's the compatibility like on semi-colons or pipe symbols? The latter should still look okay, would still need to be careful with command line but would those be otherwise okay? Not that I massively need compatibility as I'm only working with other Mac users for now, but that may change so it's better to get it sorted now :)
     
  7. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #7
    You can use any characters but the slash. If you want to use the pipe character or quote characters or any other character with a special meaning they must be escaped (preceded with a backslash).
     

Share This Page