Mkdir
| Company type | Command |
|---|---|
| Industry | Computing / Operating systems |
| Founded | Early Unix distributions (4.1BSD and System V) |
| Products | Directory creation command |
mkdir (short for make directory) is a command utilized across various operating systems such as Unix, DOS, DR FlexOS, IBM OS/2, Microsoft Windows, and ReactOS to generate new directory folders. It is also accessible within the EFI shell environment and the PHP programming language. In environments like DOS, OS/2, Windows, and ReactOS, this command is frequently shortened to md.
This command serves a similar purpose to the Stratus OpenVOS create_dir instruction. Equivalent commands for directory creation are found in MetaComCo TRIPOS and AmigaDOS (under the name MakeDir). Furthermore, numerical computing platforms such as MATLAB and GNU Octave provide an mkdir function that performs the same operation.
History
In early Unix distributions (4.1BSD and initial versions of System V), executing mkdir required elevated privileges via setuid root access because the operating system kernel did not originally include a dedicated mkdir system call. Instead, directories were created by using mknod, manually establishing the . and .. directory entries. The command has been part of the MS-DOS toolkit since version 2. Both Digital Research’s DR DOS 6.0 and Datalight’s ROM-DOS offer their own versions of the and commands.
The GNU implementation of mkdir, included in the coreutils package, was written by David MacKenzie. This version is also compatible with the open-source MS-DOS emulator DOSBox and the lightweight operating system KolibriOS.
Usage
The basic syntax is simple:
<syntaxhighlight lang="bash"> mkdir directory_name </syntaxhighlight>
Here, directory_name defines the name of the directory to be created inside the current working directory by default. On Unix-like systems and Windows with Command extensions enabled, it is possible to specify multiple directories at once; mkdir will attempt to create each one listed.
Options
On Unix-based systems, mkdir supports several options, including:
-p (--parents): Automatically constructs any missing parent directories along the given path. For instance, runningmkdir -p a/bwill create directorya, if it does not exist, before creatingbinside it. The command does not throw errors if any directories already exist.-m (--mode): Assigns specific octal permission settings to newly created directories. This option is commonly employed in shell scripts to restrict access rights for temporary folders.
The -p flag is particularly valuable when establishing nested directory hierarchies, whereas -m facilitates applying precise security controls on new directories.
Examples
Example using the -p option:
<syntaxhighlight lang="bash"> mkdir -p /tmp/a/b/c </syntaxhighlight>
If the directory /tmp/a exists but /tmp/a/b does not, this command will first create the missing intermediate folder before creating /tmp/a/b/c.
A more sophisticated example (leveraging shell brace expansion) creates an entire directory tree structure in one command:
<syntaxhighlight lang="bash"> mkdir -p tmpdir/{trunk/sources/{includes,docs},branches,tags} </syntaxhighlight>
Within bash scripts, combining variables with mkdir often involves using the POSIX built-in 'eval' command:
<syntaxhighlight lang="bash"> DOMAIN_NAME=includes,docs eval "mkdir -p tmpdir/{trunk/sources/{${DOMAIN_NAME}},branches,tags}" </syntaxhighlight>
This produces a directory layout as follows:
<syntaxhighlight lang="text">
tmpdir ________|______ | | |
branches tags trunk
|
sources
____|_____
| |
includes docs
</syntaxhighlight>
See also
- Filesystem Hierarchy Standard
- GNU Core Utilities
- Find – Commonly paired with mkdir to duplicate directory trees without copying files.
- List of Unix commands
- List of DOS commands
Further reading
- Jim Cooper, Special Edition Using MS-DOS 6.22, Que Publishing, 2001
- Kathy Ivens & Brian Proffit, OS/2 Inside & Out, Osborne McGraw-Hill, 1993
- Æleen Frisch, Windows 2000 Commands Pocket Reference, O'Reilly Media, 2001
- Daniel J. Barrett, Macintosh Terminal Pocket Guide, O'Reilly Media, 2012