Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was writing a widget for displaying files in a given directory and I wondered whether I need to cover the case where a file has an empty name.

Testing showed not obvious results, so I was wondering whether someone has definite information

$ touch ""
touch: cannot touch `': No such file or directory

$ echo > ""
bash: : No such file or directory
share|improve this question
up vote 11 down vote accepted

A filename may not be empty. To quote the Single Unix Specification, §3.170, a filename is:

A name consisting of 1 to {NAME_MAX} bytes used to name a file. The characters composing the name may be selected from the set of all character values excluding the <slash> character and the null byte.

So, it must consist of at least 1 byte, i.e., not empty.

Not that from that definition, none of those characters need to be visible (i.e., could all be whitespace) nor do they need to be printing (could all be control characters). And if you're assuming file names are UTF-8, they need not be.

share|improve this answer
thanks! so i can use an empty string to denote an absent file name in my code. nice – Johannes Schaub - litb Mar 5 '13 at 20:17
The only real restriction is that the name can't contain ASCII '\0' nor '/'. – vonbrand Mar 5 '13 at 20:52
@vonbrand Yep, I made sure to copy that sentence (even though it doesn't directly address the question) and then reiterate underneath to be sure OP knows that, as its quite relevant for trying to display file names. PS: Thanks for fixing that in the edit. – derobert Mar 5 '13 at 20:57

I don't think you can have a file without a name. However, you might want to handle the (rather contrived) case of a file name that consists of nothing but spaces, which are possible:

$ echo "Hi" > "          "
$ echo "Bye" > "         "
$ cat "         "
$ cat "          "
share|improve this answer
We once had a file whose name was the escape sequence for "previous line; start of line" plus some junk. Doing an ls some file wasn't there, ls missing showed it just fine... for extreme puzzlement of the perpetrator. – vonbrand Mar 5 '13 at 20:55

Interesting. Looks like you can do whitespace-only names at least:

$ touch " "
$ ls -l
total 0
-rw-r--r-- 1 user user 0 Mar  5 22:57
$ file \
 : empty
share|improve this answer

You should. I don't know how, but I created a file with an empty file name, I doubt there was a whitespace there :

chaouche@karabeela /mnt/ubuntu/storage $ ls
total 352K
drwx------  3 chaouche chaouche 4.0K Feb 12 11:00
drwxr-xr-x  4 chaouche chaouche 4.0K Apr 14 23:13 AUDIO
drwxr-xr-x  5 chaouche chaouche 4.0K Jul 20 22:25 BACKUPS
-rw-r--r--  1 chaouche chaouche 310K Jun 15 13:00 cv_yassine_chaouche_2012.pdf
drwxr-xr-x  2 chaouche chaouche 4.0K Jun 10 22:16 Mageia-3-i586-DVD
drwxr-xr-x  5 chaouche     1001 4.0K Oct 12  2012 MUSIQUE
drwxr-xr-x 15 chaouche chaouche 4.0K Oct 27  2012 PARISVIII
drwxr-xr-x  6 chaouche chaouche 4.0K Jun 12 18:33 SABAYON
drwxr-xr-x  5 chaouche chaouche 4.0K Jun 15 13:23 SIFTECH
drwxr-xr-x  5 chaouche chaouche 4.0K Feb  6 15:11 TOILES
drwxr-xr-x  4 chaouche chaouche 4.0K Jul 17 17:21 VIDEOS
chaouche@karabeela /mnt/ubuntu/storage $

I asked on #linux how to remove the file and a person suggested I moved all other files away then use the tab completion, which gave a very interesting behaviour :

# with tab completion
chaouche@karabeela /mnt/ubuntu/storage $ ls /
total 45M
-rwxr-xr-x  1 chaouche chaouche  34M Jan 16  2013 inkscape-0.48.4-1-win32.exe
-rwxr-xr-x  1 chaouche chaouche 8.6M Feb  4 11:42 mypaint-1.0.0-win32-installer.exe
-rwxr-xr-x  1 chaouche chaouche 2.7M Jan 17  2013 pdftkb_setup.exe

# without tab completion, wrote "/" by hand
chaouche@karabeela /mnt/ubuntu/storage $ ls /
total 212K
-rw-r--r--   1 root root    0 Apr 15  2012 1
drwxr-xr-x   2 root root 4.0K Dec  9  2012 bin
drwxr-xr-x   3 root root 4.0K Jun 15 12:10 boot
-rw-------   1 root root  74K Jul  1  2011 dead.letter
drwxr-xr-x  19 root root 4.2K Jul 20 20:14 dev
drwxr-xr-x 117 root root  12K Jul 20 21:30 etc
drwxr-xr-x   4 root root 4.0K Jun 12 18:40 home
drwxr-xr-x   2 root root 4.0K Jun  8  2011 initrd
drwxr-xr-x  20 root root  12K Dec  9  2012 lib
drwx------   2 root root  16K Jun  7  2011 lost+found
drwxr-xr-x   4 root root 4.0K Jul  3 01:56 media
drwxr-xr-x   9 root root 4.0K Apr 15 00:06 mnt
drwxr-xr-x   8 root root 4.0K Jun 30 23:19 opt
dr-xr-xr-x 198 root root    0 Jun 15 13:10 proc
drwxr-x---  28 root root 4.0K Jul 20 21:42 root
drwxr-xr-x   2 root root  12K Dec  9  2012 sbin
drwxr-xr-x   2 root root 4.0K Apr  3  2011 srv
drwxr-xr-x  12 root root    0 Jun 15 13:10 sys
-rw-r--r--   1 root root    0 Jun 12 18:40 thisismageia
drwxrwxrwt  69 root root  36K Jul 20 22:04 tmp
drwxr-xr-x  14 root root 4.0K Nov  6  2011 usr
drwxr-xr-x  18 root root 4.0K Jul  2  2011 var
chaouche@karabeela /mnt/ubuntu/storage $

If there was a whitespace in the filename it would have been replaced by \" " when I hit the tab key, but it didn't.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.