What are reserved filenames for various platforms?

六月ゝ 毕业季﹏ 提交于 2019-11-27 09:14:52
Jacob T. Nielsen

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

Full description of legal and illegal filenames on Windows: http://msdn.microsoft.com/en-us/library/aa365247.aspx

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.

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

The boost::filesystem Portability Guide has a lot of good info.

Well, for MSDOS/Windows, NUL, PRN, LPT<n> and CON. They even cause problems if used with an extension: "NUL.TXT"

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!