问题
I'm not asking about general syntactic rules for file names. I mean gotchas that jump out of nowhere and bite you. For example, trying to name a file "COM<n>" on Windows?
回答1:
From: http://www.grouplogic.com/knowledge/index.cfm/fuseaction/view_Info/docID/111.
The following characters are invalid as file or folder names on Windows using NTFS:
/
?
<
>
\
:
*
|
"
and any character you can type with the Ctrl key.In addition to the above illegal characters the caret
^
is also not permitted under Windows Operating Systems using the FAT file system.Under Windows using the FAT file system file and folder names may be up to 255 characters long.
Under Windows using the NTFS file system file and folder names may be up to 256 characters long.
Under Window the length of a full path under both systems is 260 characters.
In addition to these characters, the following conventions are also illegal:
- Placing a space at the end of the name
- Placing a period at the end of the name
The following file names are also reserved under Windows:
aux
,com1
,com2
,- ...
com9
,lpt1
,lpt2
,- ...
lpt9
,con
,nul
,prn
回答2:
Full description of legal and illegal filenames on Windows: http://msdn.microsoft.com/en-us/library/aa365247.aspx
回答3:
As others have said, device names like COM1 are not possible as filenames under Windows because they are reserved devices.
However, there is an escape method to create and access files with these reserved names, for example, this command will redirect the output of the ver
command into a file called COM1:
ver > "\\?\C:\Users\username\COM1"
Now you will have a file called COM1 that 99% of programs won't be able to open, and will probably freeze if you try to access.
Here's the Microsoft article that explains how this "file namespace" works. Basically it tells Windows not to do any string processing on the text and to pass it straight through to the filesystem. This trick can also be used to work with paths longer than 260 characters.
回答4:
A tricky Unix gotcha when you don't know:
Files which start with - or -- are legal but a pain in the butt to work with, as many command line tools think you are providing options to them.
Many of those tools have a special marker "--" to signal the end of the options:
gzip -9vf -- -mydashedfilename
回答5:
The boost::filesystem Portability Guide has a lot of good info.
回答6:
Well, for MSDOS/Windows, NUL, PRN, LPT<n> and CON. They even cause problems if used with an extension: "NUL.TXT"
回答7:
Unless you're touching special directories, the only illegal names on Linux are '.
' and '..
'. Any other name is possible, although accessing some of them from the shell requires using escape sequences.
EDIT: As Vinko Vrsalovic said, files starting with '-
' and '--
' are a pain from the shell, since those character sequences are interpreted by the application, not the shell.
来源:https://stackoverflow.com/questions/122400/what-are-reserved-filenames-for-various-platforms