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!!

6
  • 3
    Which characters are allowed or disallowed depends on the filesystem in use. Commented Oct 1, 2012 at 2:51
  • @JimParis Do you have an example of a *nix filesystem that doesn't allow characters other than \0 and /? Commented Oct 1, 2012 at 4:43
  • Also interesting in this context: Understanding Unix file name encoding Commented Oct 1, 2012 at 7:07
  • @JimParis you were right. Commented Oct 1, 2012 at 13:27
  • 1
    "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?" Just try to create the file, and fail cleanly and tell them if it didn't work. There are other reasons file creation could fail (creating under a nonexistent directory, for example), and trying to check for all of them in advance just opens you to a race condition. Commented Oct 1, 2012 at 15:14

2 Answers 2

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

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.