Good style/practices for separators in file (or directory) names

11,710

Solution 1

Short answer: "Foo_-_Bar"

Long answer:

To make things easy to spot I tend to use a sequence of characters in areas where this is needed, the idea is to use something that is easy to spot so you get that this is the separator maybe something like "_-_" or "___".

A practical example where I use this is my mp3 collection where the filenames contain artist and song title, and sometimes the sequence number. And if you use a magic sequence to separate them it is easy both for the eye and for the scripts. The mp3 example could look something like this.

  • 01_Blue_Man_Group_-_Above.mp3
  • 02_Blue_Man_Group_-_Time_to_Start.mp3
  • 03_Blue_Man_Group_-_Sing_Along.mp3

Now this can be translated into your example if Foo and Bar is two logical things that should not mix, and that could then be Foo_-_Bar.

Solution 2

With so many characters that you might not think should be special, in fact being special, I just use the special characters anyway. This also puts me in the good habits of using bash completion, where it will auto-escape all the special characters in a filename. But it also puts me in the good habits of escaping/quoting EVERYTHING in scripts and multi-part 1-liners in bash.

For example, in just a simple 1-liner:

for file in *.txt; do something.sh "$file"; done

That way, even if one of the files has a space, or some other character, the do part of the loop will still act on it, and not miss on 2 or more file-name-parts, possibly causing unintended side-effects.

Since I cannot control the space/not-space naming of EVERY file I encounter, and if I tried, it would probably break some symlinks somewhere, causing yet more unintended consequences, I just expect that all filename/directoryname could have spaces in it, and just quote/escape all variables to compensate.

So, then I just use whatever characters I want (often spaces) in filenames.

I even use spaces in ZFS dataset names, which I have to admit has caused a fair amount of head-scratching among the developers that write the software for the NAS I use.

Sum-up: Spaces are not an invalid character, so there's no reason not to use them.

Solution 3

I use dashes -, for the reasons you mention above. I avoid underscores because they require using the shift key, so take at least twice as long to type (also, I think they're ugly)

I'm more inclined to do this for script filenames than anything else - I guess it's more important to me to clearly identify what a script is for. Document files are just inert data, but scripts are potentially dangerous if misused.

Solution 4

I use underscores (_). Visually they vanish, and are easy to type and not special.

Share:
11,710
BorrajaX
Author by

BorrajaX

Updated on September 18, 2022

Comments

  • BorrajaX
    BorrajaX almost 2 years

    I'm not exactly sure if this is a "right" question to post here. I'm probably asking more about "opinions" than actual categorical answers (of those that either work or don't, and that's it).

    I was wondering what name separators would be the most linux friendly (or more specifically, Bash friendly) and human friendly at the same time.

    Let's say I want to create a directory that contains something related to Mr. Foo Bar (Foo being the first name, Bar being the last name)

    Having the name "Foo-Bar/" is very convenient. - is a "regular" character, it doesn't need to be escaped, it clearly shows that Foo and Bar are two separate things... Nice.

    Now, "Foo.Bar" is a bit trickier. Someone may think that Foo.Bar is actually a file (at a first glance, specially if you don't have terminals with coloring enabled) where "Foo" is the filename, and "Bar" the extension.

    I could also use "Foo Bar", but then I need to escape the whitespace when I want to access the directory and, if I want to list the contents of the parent directory (where Foo Bar is located) and put said list in a bash array, the white space is going to cause trouble (a lot). Not nice.

    Brackets () also cause a lot of issues. They also need to be escaped, then to cause trouble with commands as scp... Not nice.

    So... the question (at last) is: If you need to make the name of a file clear and meaningful at a fist glance, and you need to use separators, what do you use?

    • Admin
      Admin almost 12 years
    • Admin
      Admin almost 12 years
      I often use dashes, but it depends on what I am doing. Some may consider camel case as appropriate like FooBar.
  • tshepang
    tshepang almost 12 years
    me find them uglier than -, and harder to type (one has to use shift key)
  • lynxlynxlynx
    lynxlynxlynx almost 12 years
    And this way you can also distinguish names that already contain dashes (or separators in general).
  • DanDan
    DanDan over 8 years
    Also really nice to reg ex parse! : ^(.+)_-_(.+)\.mp3$
  • ThorSummoner
    ThorSummoner over 8 years
    Given that humans would view these files, this is a good answer; In essence I would consider this a separator string; A multi-character unique separator. In Vagrant, i've noticed it uses the string VAGRANTSLASHin file names where the separation is.