Using underscore in file names?

I use the command line frequently to navigate my files so I try not to have spaces in file names. Typically I have used an underscore to connect words but it was recently suggested that I should use a dash. Are there any disadvantages to using an underscore in file names? Should I switch to a dash? My system is running Xubuntu and I almost exclusively use the bash shell.

Thanks


As far as the operating system is concerned they both are as usable as each other.

One thing I would note - some software (such as some video playing systems - XBMC etc) will automatically replace underscores with spaces when displaying files to make them look nicer.

For that reason alone I'd be tempted to stick to underscores.

Also, hyphens are used for switches to commands, so it may be a little confusing having hyphens in you filenames as well as in your command switches.

Other than that it's purely down to personal choice.


Dash doesn't require you to press SHIFT, meaning you type faster. That is the only other benefit I can think of above what Mr. Jenkins has suggested.

Likely the person making that suggestion is an avid Vim user and wishes to do everything with the least amount of keyboard movement.


My recommendation is to use underscores instead of spaces. Underscores are usually the convention that people use when replacing spaces, although hyphens are fine too I'd say. But since hyphens might show up in other ways such as hyphenated words, you'll have more success in preserving a name value by using underscores. For instance, if you have a file called "A picture taken in Winston-Salem, NC.jpg" and you want to convert the spaces to underscores, then you can preserve the hyphen in the name and retain its meaning.

Spaces cause problems for people who want to use the command line in advanced ways such as in for loops like this:

for file in *.mp3 ; do mpg321 $file -w - | oggenc -o ${file%%.mp3}.ogg - ; done

If any of the mp3 files matched by that wildcard have spaces in their name, it will cause the filename to be broken up into sections instead of the whole. You can get around this by changing the BASH shell's IFS variable or using the find command, but its an annoyance and a lot of people don't know this, so it can cause problems.

I doubt this or even a bumper sticker campaign would prevent people from putting spaces in filenames, but if you want to help yourself, more power to you and thank you on behalf of those of us who care.


The only reason you should consider using hyphens instead of underscores is for basic readability. Frequent underscores tend to distort reading, as it changes the line of your vision from the center of the y-axis to the bottom. Hyphens however make it easier on the eye.

As far as the operating system is concered, both characters are equally valid, and don't require escaping (which spaces do, and hence are a pain to work with).


I personally prefer hyphens over underscore because you don't need to hold down shift key for them. But one thing to remember is that if you are primarily doing python coding - and your code tree has files/directories with hyphen in them and you intend to use them as modules (do an import filename in your code), then this will cause errors as python modules cannot have hyphen in them. Of course, there are roundabout ways of importing modules which have hyphen but I'd stick with underscore as it also follows the PEP-8 guidelines.

So for most of my bin/ programs or aliases I use hyphens but when it comes to writing python programs/modules - I use underscores.