Files with empty names

filenames

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

Best Answer

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.

Related Question