linux - [Solved-4 Solutions] What characters are forbidden in Windows and Linux directory names - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

What characters are forbidden in Windows and Linux directory names ?

Linux - Solution 1:

  • Fforbidden filename characters is not going to work on Windows because it reserves filenames as well as characters.
  • Yes, characters like * " ? and others are forbidden, but there are a infinite number of names composed only of valid characters that are forbidden.
  • For example, spaces and dots are valid filename characters, but names composed only of those characters are forbidden.
  • Windows does not distinguish between upper-case and lower-case characters, so you cannot create a folder named A if one named a already exists.
  • Allowed names like PRN and CON, and many others, are reserved and not allowed.
  • Windows also has several length restrictions; a filename valid in one folder may become invalid if moved to another folder.
  • The rules for naming files and folders is on MSDN.
  • In general, use user-generated text to create Windows directory names.
  • If you want to allow users to name anything they want, you have to create safe names like A, AB, A2 et al., store user-generated names and their path equivalents in an application data file, and perform path mapping in your application.
  • If you absolutely must allow user-generated folder names, the only way to tell if they are invalid is to catch exceptions and assume the name is invalid.
  • Even that is fraught with peril, as the exceptions thrown for denied access, offline drives, and out of drive space overlap with those that can be thrown for invalid names.

Linux - Solution 2:

1. The forbidden printable ASCII characters are:

Linux/Unix:

/ (forward slash)
click below button to copy the code. By - Linux tutorial - team

Windows:

< (less than)
> (greater than)
: (colon - sometimes works, but is actually NTFS Alternate Data Streams)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
click below button to copy the code. By - Linux tutorial - team

2. Non-printable characters

Linux/Unix:

0 (NULL byte)
click below button to copy the code. By - Linux tutorial - team

Windows:

0-31 (ASCII control characters)
click below button to copy the code. By - Linux tutorial - team

Note: While it is legal under Linux/Unix file systems to create files with control characters in the filename, it might be a nightmare for the users to deal with such files.

3. Reserved file names

The following filenames are reserved:

Windows:

CON, PRN, AUX, NUL 
COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9
LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9
click below button to copy the code. By - Linux tutorial - team

4. Other rules

Windows:

Filenames cannot end in a space or dot.
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 3:

  • Under Linux and other Unix-related systems, there are only two characters that cannot appear in the name of a file or directory, and those are NUL '\0' and slash '/'.
  • The slash, of course, can appear in a path name, separating directory components.
  • Rumour1 has it that Steven Bourne (of 'shell' fame) had a directory containing 254 files, one for each single letter (character code) that can appear in a file name.
  • It was used to test the Bourne shell, and routinely wrought havoc on unwary programs such as backup programs.
  • Other people have covered the Windows rules.
  • Note that MacOS X has a case-insensitive file system.

Linux - Solution 4:

  • Instead of creating a blacklist of characters, you could use a whitelist.
  • All things considered, the range of characters that make sense in a file or directory name context is quite short, and unless you have some very specific naming requirements your users will not hold it against your application if they cannot use the whole ASCII table.
  • It does not solve the problem of reserved names in the target file system, but with a whitelist it is easier to mitigate the risks at the source.
  • In that spirit, this is a range of characters that can be considered:
    • Letters (a-z A-Z) - Unicode characters as well, if needed
    • Digits (0-9)
    • Underscore (_)
    • Hyphen (-)
    • Space
    • Dot (.)
  • Name must contain at least one letter or number (to avoid only dots/spaces)
  • Name must start with a letter or number (to avoid leading dots/spaces)

This already allows quite complex and nonsensical names. For example, these names would be possible with these rules, and be valid file names in Windows/Linux:

  • A...........ext
  • B -.- .ext

Related Searches to - linux - linux tutorial - What characters are forbidden in Windows and Linux directory names