Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

3 Answers

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 at 20:17
The only real restriction is that the name can't contain ASCII '\0' nor '/'. – vonbrand Mar 5 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 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 "         "
Bye
$ cat "          "
Hi
share|improve this answer
1  
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 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

Your Answer

 
discard

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.