1

I was under the impression that the only character not allowed in file names was /, but I don't seem to be able to create a file whose name contains characters such as *, \, ", :, |, < or > either. For instance,

$ echo "*"
> *
$ touch "*"
> touch: setting times of `*': No such file or directory

I'm writing a shell script that requires the user to type a file name and I would like to make sure the name doesn't have any invalid characters. Is there a list somewhere?

Update:

I didn't realise I was doing this in a FAT32 filesystem (USB stick), which does not allow `*' and other characters in file names. No wonder it didn't work. Sorry, my bad!!

daisy
  • 54,555

2 Answers2

6

I'm pretty sure the only reserved bytes are 0 (ASCII Nul) and 0x2f (ASCII '/', forward slash).

You can easily make a file with '.', '\' and other funky things in it. Think "unicode file names", which contain all kinds of weird byte values.

Naturally, you can't have duplicate file names in the same directory, so files named "." and ".." can't usually be made by a mere user: the filesystem part of the kernel creates them when a directory gets created.

Also note that most filesystems have a limit on the length of a file name, and you can't create a file whose name is the empty string.

1

Well actually you could, just make it a single quote,

e.g touch '*'

Or use your FM:

enter image description here

For more details, see the Reserved characters and words on this page, at least / \ . .. are not allowed

daisy
  • 54,555
  • Interesting, with single quotes works. What is wrong with the double quotes? (nice icons btw :) – Ernest A C Oct 01 '12 at 00:03
  • 1
    They stop the shell from treating meta-characters differently. http://mywiki.wooledge.org/Quotes – llua Oct 01 '12 at 00:44
  • @llua But the echo "*" command showed that the shell wasn't treating * specially within double quotes. Something odd is going on here. – Kyle Jones Oct 01 '12 at 02:23
  • @llua @don_crissti Yes, the double quotes should prevent the * from being treated especially, just like single quotes. – Ernest A C Oct 01 '12 at 13:21
  • 1
    It wasn't the quotes but the filesystem (FAT32) which does not allow `*' in file names. Mystery solved! – Ernest A C Oct 01 '12 at 13:26