mkdir

The mkdir (make directory) command in the Unix, DOS, FlexOS[1], OS/2, Microsoft Windows, and ReactOS operating systems is used to make a new directory. It is also available in the EFI shell[2] and in the PHP scripting language. In DOS, OS/2, Windows and ReactOS, the command is often abbreviated to md.

The command is analogous to the OpenVOS create_dir command.[3] AmigaDOS provides a MakeDir command to create new directories.[4]

Usage

Normal usage is as straightforward as follows:

mkdir name_of_directory

where name_of_directory is the name of the directory one wants to create. When typed as above (i.e. normal usage), the new directory would be created within the current directory. On Unix and Windows (with Command extensions enabled,[5] the default [6]), multiple directories can be specified, and mkdir will try to create all of them.

Options

On Unix-like operating systems, mkdir takes options. The options are:

  • -p: parents or path, will also create all directories leading up to the given directory that do not exist already. For example, mkdir -p a/b will create directory a if it doesn't exist, then will create directory b inside directory a. If the given directory already exists, ignore the error.
  • -m: mode, specify the octal permissions of directories created by mkdir.

-p is most often used when using mkdir to build up complex directory hierarchies, in case a necessary directory is missing or already there. -m is commonly used to lock down temporary directories used by shell scripts.

Examples

An example of -p in action is:

mkdir -p /tmp/a/b/c

If /tmp/a exists but /tmp/a/b does not, mkdir will create /tmp/a/b before creating /tmp/a/b/c.

And an even more powerful command, creating a full tree at once (this however is a Shell extension, nothing mkdir does itself):

mkdir -p tmpdir/{trunk/sources/{includes,docs},branches,tags}

If one is using variables with mkdir in a bash script, POSIX `special' built-in command 'eval' would serve its purpose.

DOMAIN_NAME=includes,docs
eval "mkdir -p tmpdir/{trunk/sources/{${DOMAIN_NAME}},branches,tags}"

This will create:

          tmpdir
    ________|______
   |        |      |
branches   tags  trunk
                   |
                 sources
               ____|_____
              |          |
          includes     docs

History

In early versions of Unix (4.1BSD and early versions of System V), this command had to be setuid root as the kernel did not have an mkdir syscall. Instead, it made the directory with mknod and linked in the . and .. directory entries manually.

See also

References

  1. http://www.bitsavers.org/pdf/digitalResearch/flexos/1073-2003_FlexOS_Users_Guide_V1.3_Nov86.pdf
  2. "EFI Shells and Scripting". Intel. Retrieved 2013-09-25.
  3. http://stratadoc.stratus.com/vos/19.1.0/r098-19/wwhelp/wwhimpl/common/html/r098-19.pdf
  4. https://archive.org/details/1988-rugheimer-spanik-amigados-quick-reference
  5. "Microsoft Windows XP - Mkdir". Microsoft. Archived from the original on July 22, 2016. Retrieved 25 October 2012.
  6. "Microsoft Windows XP - Cmd". Microsoft. Retrieved 25 October 2012.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.