Linux / UNIX: Rules For Naming File And Directory Names

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:

ADVERTISEMENTS

  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:

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
20 comments… add one
  • KarthiKeyan Feb 19, 2007 @ 20:23

    i can not execute the following shell command…

    “cd directory name with spaces”

  • SutantoKurniawan Jun 27, 2007 @ 17:25

    Escape the spaces with “\”.
    eg.

    “cd Directory\ Name\ With\ Spaces”

    • littlebear May 16, 2010 @ 3:11

      Thanks for reminding my sluggish memory :P

  • RoHaN Aug 27, 2007 @ 10:24

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

    SD/-
    RoHaN.

  • Joe Feb 29, 2008 @ 18:16

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

    cd “directory name with spaces”

  • bebby Feb 19, 2009 @ 9:28

    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.

    • Pihu Apr 11, 2011 @ 9:19

      Thanks Bebby, I was not aware of this concept.

  • bebby Feb 19, 2009 @ 9:31

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

  • Nick Sep 9, 2009 @ 21:31

    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…

  • SJari Apr 11, 2010 @ 18:08

    what about folders with ()

    like # cd (name) dose not work

    • Paul May 5, 2010 @ 4:44

      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.

  • Maurits Oct 1, 2010 @ 17:19

    What about hyphens, parentheses or brackets?

  • Terry Bruce Dec 4, 2011 @ 8:51

    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.

  • vinothraja Feb 13, 2012 @ 14:16

    Pls help me
    i am a beginner in shell scripting ..So pls help me in following scenario

    i have made a shell script named as “xxxx.sh”

    this is has to run twice a day

    the o/p file must be in “xxxx_ddmmyy_HH:SS.txt”

    the o/p creates problem since the ” : ” used in naming a txt file.(ie HH:MM)

    so is there any way of changing the time format like HH.MM or HH-MM

    i need both time & date in the txt file name

    so how to proceed

    • Paul judd Feb 14, 2012 @ 5:20

      vinothraja
      have you tried with back slash escape of the ‘:’, that is
      cat >> ‘somepath/xxxx_ddmmyy_HH\:SS.txt’
      cheers
      Paul

  • joseph Jul 10, 2012 @ 6:22

    how about I create a folder in usr/local and make a folder free games.. How can i create a folder like that?

    • 🐧 nixCraft Jul 10, 2012 @ 10:52

      @joseph,

      Try:

      cd /usr/local
      sudo mkdir "free games"
      cd "free games"

      OR

      su -
      mkdir "/usr/local/free games"
      cd "/usr/local/free games"
  • sherin Nov 20, 2012 @ 12:24

    assume the ‘usr’ directory contain ‘book’ directory and ‘home’ directory also contain ‘book’ directory. if we are in the directory ‘she’ in ‘etc’, then what will be the result for cd ~/book. there are two book directories

  • Shortfella Jul 8, 2013 @ 16:44

    I need an advice. I installd my friend Fedora, but he understands nothing in linux and tends to delete useful files to which, for example, cups refer. I want to hide them by adding “.” before the file name. I can’t test it, so I’m asking here. Will that affect programs that refers to those files if I hide them?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.