How to create a directory with '/' in unix?

5,734

Solution 1

The character / is not allowed to appear in file names, including directory names. It is always interpreted as a separator between pathname components.

Sources:

Pathname

A string that is used to identify a file. In the context of POSIX.1-2008, a pathname may be limited to {PATH_MAX} bytes, including the terminating null byte. It has optional beginning <slash> characters, followed by zero or more filenames separated by <slash> characters. A pathname can optionally contain one or more trailing <slash> characters. Multiple successive <slash> characters are considered to be the same as one <slash>, except for the case of exactly two leading <slash> characters.

Note:

If a pathname consists of only bytes corresponding to characters from the portable filename character set (see Portable Filename Character Set), <slash> characters, and a single terminating <NUL> character, the pathname will be usable as a character string in all supported locales; otherwise, the pathname might only be a string (rather than a character string). Additionally, since the single-byte encoding of the <slash> character is required to be the same across all locales and to not occur within a multi-byte character, references to a <slash> character within a pathname are well-defined even when the pathname is not a character string. However, this property does not necessarily hold for the remaining characters within the portable filename character set.

Pathname Resolution is defined in detail in Pathname Resolution.

(The Open Group Base Specifications Issue 7, volume Base Definitions, chapter 3 "Definitions", s.v. 3.271 Pathname)

Solution 2

While / (U+002F) is the path component separator and can't be used in file names obviously, there are a few ( for instance, even a combining one (̸)) that look very similar and that you could use instead if the intent is to confuse or fool people using the system.

For instance:

mkdir A3456⁄90876⁄IN

Now, if you just want to create a IN directory inside a 90876 directory inside a A3456 directory using one single command, then it's just:

mkdir -p A3456/90876/IN

Solution 3

You can't. It's explicitly prohibited per the POSIX definition of a path/file name:

3.170 Filename

A sequence of bytes consisting of 1 to {NAME_MAX} bytes used to name a file. The bytes composing the name shall not contain the <NUL> or <slash> characters. In the context of a pathname, each filename shall be followed by a <slash> or a <NUL> character; elsewhere, a filename followed by a <NUL> character forms a string (but not necessarily a character string). The filenames dot and dot-dot have special meaning. A filename is sometimes referred to as a "pathname component". See also Pathname

Share:
5,734

Related videos on Youtube

Gilles 'SO- stop being evil'
Author by

Gilles 'SO- stop being evil'

Updated on September 18, 2022

Comments

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 2 years

    I want to create a directory with name as below

    A3456/90876/IN AS%90876 JI KOL
    
    • John N
      John N over 7 years
      Why do you want to do this? (As others have noted, it's not possible to have "/" in filenames. However, if you tell us why you want to do this someone may be able to suggest a "work around").
    • magor
      magor over 7 years
      / is more or less the one and only character which you cannot have in the filename...and the null character which is kindof terminating the filename
    • Wildcard
      Wildcard over 7 years
      You can't do this...BUT, in the macOS graphical file browser, "Finder," literal colons in a file or directory name are displayed as /. So you can type in a / in a file name in the Finder, but the way that is actually stored is a :. (And, you can't type in a : in a file name in the Finder, and any : present in the actual name will be displayed by Finder as a /.)
  • ozzy
    ozzy over 7 years
    Like the greek question mark developer trolling right? ;) Question mark (;) same usage as in English, but note that it looks like the English semicolon! - starecat.com/content/wp-content/uploads/…
  • done
    done about 6 years
    Beside the code-points you propose: U+2044 U+2215 U+2571 U+27CB U+29F8 you may add U+337 U+338 U+FF0F. But either your proposed chars or all of them could not be encoded in a code-page that generates zeros. An Unix filename must not include NULs. For example UTF32 use a lot of NULs. Testing all possible encoding in iconv I find that only this list GB18030 ISO-IR-193 OSF05010001 UTF7 UTF8 meet the requirements of being able of encoding all chars and also avoid the use of NUL. Any other encoding fail.
  • Sridhar Sarnobat
    Sridhar Sarnobat almost 3 years
    Now I understand why gradle uses :