Using underscore in file names?

27

8

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

DQdlM

Posted 2011-04-04T17:09:09.507

Reputation: 1 831

1

Related post on SO: Why are underscores better than hyphens for file names?.

– codeforester – 2018-04-12T20:55:08.527

1If you will use LaTeX you may have to protect the filename inside a couple of brackets {...}. In case of automatic script you should consider it. The _ is used to create a subscript and have to be included e.g. in $...$ to avoid error, but it will generate a text different from what you are expecting. In the enhanced terminal of gnuplot too.. – Hastur – 2018-04-12T22:18:09.830

Answers

24

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.

Majenko

Posted 2011-04-04T17:09:09.507

Reputation: 29 007

1I use hythens, but that's because I like the look better. To technical reason behind it. – beatgammit – 2011-04-04T17:36:32.100

Thanks! Thats what I suspected but I have also found that going on my suspicions alone can get me into trouble. – DQdlM – 2011-04-04T18:34:49.227

16

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.

LawrenceC

Posted 2011-04-04T17:09:09.507

Reputation: 63 487

+1 For this same reason I use underscores with all-caps names, like FOO_BAR (I don't use caps lock, so it's nice to be able to just hold down Shift the whole time). – Richard Hansen – 2014-11-18T04:22:01.903

@DanielBeck I actually have that in my bash aliases, as well as g for git :) – Jan Warchoł – 2015-03-19T10:12:51.930

I believe vim considers two words seperated by hyphen as a single word, so this could be a reason to use underscores in file names as a vim user. – oddRaven – 2016-08-16T11:04:48.807

7If that were the case they'd use vi instead - one less keystroke ;) – Majenko – 2011-04-04T17:39:34.757

4@Matt it's alias'ed to v anyway ;-) – Daniel Beck – 2011-04-04T18:24:52.823

7

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.

deltaray

Posted 2011-04-04T17:09:09.507

Reputation: 1 665

1I agree totally. A well constructed filename needs no spaces. – DQdlM – 2011-04-04T18:33:04.220

4@KennyPeanuts - but a well written program doesn't choke on spaces. – detly – 2011-04-05T05:27:38.983

2@detly - touche' – DQdlM – 2011-04-05T10:03:40.567

1@detly No, a good programmer understands that there have to be limits in what you can do and should do. If you say it shouldn't choke on spaces, then someone could one up that and say it shouldn't choke on end of line, then someone would say it shouldn't choke on /, then someone could say it shouldn't choke on end of file. There is an old saying "Formula for failure: try to please everyone". – deltaray – 2011-04-05T15:11:27.603

1The filesystem spec tells you what to code to, no more, no less. On Ext2/3/4, for example, a filename may contain any character except \0 or \\\\. Coding to anything less than that is broken by design. So, no, it shouldn't choke on spaces, or EOL, or non-ASCII characters, or anything that isn't \0 or \\. Using library functions for file path manipulation is the way to go, because many other people have already solved these problems. – detly – 2011-04-06T00:55:07.910

@detly - the forum software was unable to correctly render the special characters in your comment. I guess it's broken by design?(insert smiley here...) Ok, perhaps it's a user error -- always a fine line, user-error vs software-error. The point is that "design flaws" and "implementation flaws" are separate issues; bugs (implementation errors, intentional omissions) can be fixed incrementally over time. Design flaws can not. Handling spaces in filenames is a feature that can (and should) be implemented in v2.0 of a script, without necessitating a design change. (Or we can agree to disagree.) – michael – 2012-06-02T08:00:35.903

Jeff Atwood gives one more reason to prefer hyphens to underscores here: http://www.codinghorror.com/blog/2006/04/of-spaces-underscores-and-dashes.html

– chandra – 2013-12-02T16:28:30.377

@chandra you should make this an answer. It makes and interesting and good point. – DQdlM – 2014-03-21T15:42:06.170

Didn't think about the regex perspective on this and that's interesting. I guess it depends on what you want underscores to functionally help you do. For instance, on many X windows terminal emulators you can configure which characters are considered part of a word selection on double click. Some might not have a hyphen as part of that set. I don't like to use hyphens for spaces because they are more visually intrusive. But I guess its better than using spaces. – deltaray – 2014-04-28T13:44:11.830

5

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).

Varun Madiath

Posted 2011-04-04T17:09:09.507

Reputation: 383

2I find the basic readability reason to be purely subjective. I find underscores easier to read because i can pretty much visually ignore them- not read them- while dashes I actually have to read them. – goliatone – 2014-08-21T13:37:48.843

3

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.

Saurabh Hirani

Posted 2011-04-04T17:09:09.507

Reputation: 131

2

There are good reasons to use hyphens in any web-based context which maps to a Unix file system.

URL               What search indexes see
foo-baz-bang      foo baz bang
foo_baz_bang      foobazbang

As a web developer, this has compelled me to stick with hyphens when separating words.

rich remer

Posted 2011-04-04T17:09:09.507

Reputation: 129

1

The context mentioned above is probably related to - among other things - a 2005 Matt Cutts blog post involving Google's algorithmic preference for hyphens vs. underscores as a better indicator of a white space placeholder.

– jmmygoggle – 2019-01-11T22:14:19.267