Are there any invalid linux filenames?

40,944

Solution 1

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.

Solution 2

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.

Solution 3

Technically it's not invalid but files with dash(-) at the beginning of their name will put you in a lot of troubles. It's because it has conflicts with command arguments.

Share:
40,944
izb
Author by

izb

Twitter: http://twitter.com/izb

Updated on April 03, 2021

Comments

  • izb
    izb about 3 years

    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?