Linux / UNIX: Rules For Naming File And Directory Names

by Vivek Gite on February 17, 2006 · 13 comments

Can you specify rules for naming file and directory names under Linux and UNIX operating systems?

Following are general rules for both Linux, and Unix (including *BSD) like systems:

  1. All file names are case sensitive. So filename vivek.txt Vivek.txt VIVEK.txt all are three different files.
  2. You can use upper and lowercase letters, numbers, "." (dot), and "_" (underscore) symbols.
  3. You can use other special characters such as blank space, but they are hard to use and it is better to avoid them.
  4. In short, filenames may contain any character except / (root directory), which is reserved as the separator between files and directories in a pathname. You cannot use the null character.
  5. No need to use . (dot) in a filename. Some time dot improves readability of filenames. And you can use dot based filename extension to identify file. For example:
    • .sh = Shell file
    • .tar.gz = Compressed archive
  6. Most modern Linux and UNIX limit filename to 255 characters (255 bytes). However, some older version of UNIX system limits filenames to 14 characters only.
  7. A filename must be unique inside its directory. For example, inside /home/vivek directory you cannot create a demo.txt file and demo.txt directory name. However, other directory may have files with the same names. For example, you can create demo.txt directory in /tmp.

Linux / UNIX: Reserved Characters And Words

Avoid using the following characters from appearing in file names:

  1. /
  2. >
  3. <
  4. |
  5. :
  6. &

Please note that Linux and UNIX allows white spaces, <, >, |, \, :, (, ), &, ;, as well as wildcards such as ? and *, to be quoted or escaped using \ symbol.

See also:

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 13 comments… read them below or add one }

1 KarthiKeyan February 19, 2007

i can not execute the following shell command…

“cd directory name with spaces”

Reply

2 SutantoKurniawan June 27, 2007

Escape the spaces with “\”.
eg.

“cd Directory\ Name\ With\ Spaces”

Reply

3 littlebear May 16, 2010

Thanks for reminding my sluggish memory :P

Reply

4 RoHaN August 27, 2007

Thank You Very Much Vivek Sir For All Such Given Information Above…! :-)

SD/-
RoHaN.

Reply

5 Joe February 29, 2008

alternatively, you can use double quotes for the directory name like

cd “directory name with spaces”

Reply

6 bebby February 19, 2009

Must be very careful when creating files/ directories names with spaces.. considering a file is created with its name “tempfile with spaces” and if the same directory has another file named just “tempfile”, the contents written to the file “tempfile with spaces” will also be written to the file “tempfile”. Better avoid creating files with spaces in the filename.

Reply

7 Pihu April 11, 2011

Thanks Bebby, I was not aware of this concept.

Reply

8 bebby February 19, 2009

The above said comment is applicable only when you try to open the file without quotes as mentioned in comment by Joe.

Reply

9 Nick September 9, 2009

What are the rules for having in the same directory a file and a sub-directory having the same name? Experimenting on several Linux-es out there it seems that it’s not possible, but somehow I remember seeing this possible on a Linux / Unix-like system before…

Reply

10 SJari April 11, 2010

what about folders with ()

like # cd (name) dose not work

Reply

11 Paul May 5, 2010

cd \(name\)
works but not in some scripts I found. looking for a fool proof way coping with file names with space’s and \’s in them especially when doing
diff $A $B && rm -f $B
sort of thing.

Reply

12 Maurits October 1, 2010

What about hyphens, parentheses or brackets?

Reply

13 Terry Bruce December 4, 2011

Many thanks to SutantoKurniawan, Joe and Bebby for their advice on the spaces thing, I’m OK with MSDOS but relatively new to Linux and that one was bugging me.

Regards Terry Bruce.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 12 + 13 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: