Linux – What character can be safely used for naming files on unix/linux

filesystemslinuxunix

Before yesterday, I used only lower case letters, numbers, dot (.) and underscore(_) for directories and file naming.

Today I would like to start using more special characters. Which ones are safe (by safe I mean I will never have any problem)?

ps : I can't believe this question hasn't been asked already on this site, but I've searched for the word "naming" and read canonical questions without success (mosts are about computer names).

Edit #1 : (btw, I don't use upper case letters for file names. I don't remember why. But since a few month, I have production problems with upper case letters : Some OS do not support ascii!)

Here's what happened yesterday at work : As usual, I had to create a self signed SSL certificate. As usual, I used the name of the website for the files : www2.example.com.key www2.example.com.crt www2.example.com.csr.

Then comes the problem : Generate a wildcard self signed certificate. I did that and named the files example.com.key example.com.crt example.com.csr, which is misleading (it's a certificate for *.example.com).

I came back home, started putting some stars in apache configuration files filenames and see if it works (on a useless home computer, not even stagging).

Stars in file names really scares me : Some coworkers/vendors/… can do some script using rm find xarg that would lead to http://www.ucs.cam.ac.uk/support/unix-support/misc/horror, and already one answer talks about disaster.

Edit #2 :

Just figured that : does not need to be escaped. Anyone knows why it is not used in file names?

Edit #3 :

Let's summarize what I understood so far :

  • . and _ are safe to use in file names.
  • Special character that need to be escaped (eg space | ( * ) should be avoided.
  • Some other special caracters should be avoided for various reasons ( / - : )

What about all the other ones, like % or =?

Best Answer

Sounds like a recipe for disaster.

You can use anything other than / - but you might want to wrap in single quotes (') to avoid execution of special characters.

eg. $*`\|

Using special characters in file names is very much a Windows-ism - mostly taken from GUI machine administration versus command line (which doesn't lend itself to continuously having to escape file names).

The safest approach is just to stick to a-zA-Z0-9_.