views:

312

answers:

2

If I wanted to create a string which is guaranteed not to represent a filename, I could put one of the following characters in it on Windows:

\ / : * ? | < >

e.g.

this-is-a-filename.png

?this-is-not.png

Is there any way to identify a string as 'not possibly a file' on Linux?

+7  A: 

There are almost no restrictions - apart from '/' and '\0', you're allowed to use anything. However, some people think it's not a good idea to allow this much flexibility.

Vinay Sajip
+ it may depend on the file system being used.
Michael Krelin - hacker
+1 Thanks for the link to "Fixing Unix/Linux/POSIX Filenames".It was a really intersting read.
Ludwig Weinzierl
The flexibility gets you in trouble because a lot of applications treat filenames as a text string - and you get all sorts of character encoding problems. And that's just the obvious problem with it :)
nos
+1  A: 

An empty string is the only truly invalid path name on Linux, which may work for you if you need only one invalid name. You could also use a string like "///foo", which would not be a canonical path name, although it could refer to a file ("/foo"). Another possibility would be something like "/dev/null/foo", since /dev/null has a POSIX-defined non-directory meaning. If you only need strings that could not refer to a regular file you could use "/" or ".", since those are always directories.

mark4o