What are reserved filenames for various platforms?

前端 未结 7 1473
醉酒成梦
醉酒成梦 2020-11-28 15:00

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\" on Wi

相关标签:
7条回答
  • 2020-11-28 15:35

    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
    0 讨论(0)
  • 2020-11-28 15:35

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

    0 讨论(0)
  • 2020-11-28 15:37

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

    0 讨论(0)
  • 2020-11-28 15:40

    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
    
    0 讨论(0)
  • 2020-11-28 15:41

    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.

    0 讨论(0)
  • 2020-11-28 15:44

    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.

    0 讨论(0)
提交回复
热议问题