- How the mkdir command works in Linux
- Options for mkdir
- Making multiple directories within each other with mkdir -p
- Verify a folder is created with mkdir
- Setting the chmod or mode of a directory with mkdir
- Linux mkdir with chmod
- NAME
- SYNOPSIS
- DESCRIPTION
- OPTIONS
- OPERANDS
- STDIN
- INPUT FILES
- ENVIRONMENT VARIABLES
- ASYNCHRONOUS EVENTS
- STDOUT
- STDERR
- OUTPUT FILES
- EXTENDED DESCRIPTION
- EXIT STATUS
- CONSEQUENCES OF ERRORS
- APPLICATION USAGE
- EXAMPLES
- RATIONALE
- FUTURE DIRECTIONS
- SEE ALSO
- Linux mkdir with chmod
- setgid and mkdir
- ksh93
- OPTIONS
- /usr/bin/mkdir
- ksh93
- OPERANDS
- USAGE
- EXAMPLES
- ENVIRONMENT VARIABLES
- EXIT STATUS
- ATTRIBUTES
- /usr/bin/mkdir
- ksh93
- SEE ALSO
- Index
How the mkdir command works in Linux
On Linux and unix-like systems, like Mac, we use mkdir to make a new directory from the terminal. To do this, open up a new terminal, and make sure you’re in the right directory using the cd command. The syntax for mkdir looks like this, where dir_name is the name of your new directory, and [OPTIONS] are optional settings.
If you want to make multiple directories, just put them in curly brackets, and separate each directory by a comma. For example, the below code makes two directories called «hello» and «world»:
Options for mkdir
- -p — this allows us to make multiple directories within each other.
- -v — this outputs information on the directory or directories created.
- -m — this lets us set the chmod/mode value for our directory, i.e. 777.
Making multiple directories within each other with mkdir -p
Let’s say we wanted to create a folder structure, where we have a project folder within a parent folder, within a master folder. If we use just mkdir, we would have to make each individually. Instead, we can use mkdir -p to make them all at once.
mkdir -p master/parent/project
This will make three directories, each within the other.
Verify a folder is created with mkdir
If we want to see a message about if mkdir was successful or not, we can use mkdir -v .
The above will output the following message:
mkdir: created directory 'master'
Unfortunately, -p and -v will not work together — so you have to use one or the other.
Setting the chmod or mode of a directory with mkdir
If we want to set the mode of a directory, we can set it directly with mkdir . For example, to create a directory with a chmod value of 777 , we would write the following:
Creating multiple directories with a certain chmod or mode using mkdir
We can combine -m and -p for mkdir into one command, if we want. For example, the below code will create the folder structure master/parent/project, and set each directory to a mode of 777:
mkdir -p -v -m777 master/parent/project
Linux mkdir with chmod
NAME
SYNOPSIS
mkdir [-p][-m mode] dir.
DESCRIPTION
The mkdir utility shall create the directories specified by the operands, in the order specified. For each dir operand, the mkdir utility shall perform actions equivalent to the mkdir() function defined in the System Interfaces volume of IEEE Std 1003.1-2001, called with the following arguments: 1. The dir operand is used as the path argument. 2. The value of the bitwise-inclusive OR of S_IRWXU, S_IRWXG, and S_IRWXO is used as the mode argument. (If the -m option is specified, the mode option-argument overrides this default.)
OPTIONS
The mkdir utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines. The following options shall be supported: -m mode Set the file permission bits of the newly-created directory to the specified mode value. The mode option-argument shall be the same as the mode operand defined for the chmod utility. In the symbolic_mode strings, the op characters '+' and '-' shall be interpreted relative to an assumed initial mode of a= rwx; '+' shall add permissions to the default mode, '-' shall delete permissions from the default mode. -p Create any missing intermediate pathname components. For each dir operand that does not name an existing directory, effects equivalent to those caused by the following command shall occur: mkdir -p -m $(umask -S),u+wx $(dirname dir) && mkdir [-m mode] dir where the -m mode option represents that option supplied to the original invocation of mkdir, if any. Each dir operand that names an existing directory shall be ignored without error.
OPERANDS
The following operand shall be supported: dir A pathname of a directory to be created.
STDIN
INPUT FILES
ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of mkdir: LANG Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of IEEE Std 1003.1-2001, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.) LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments). LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES .
ASYNCHRONOUS EVENTS
STDOUT
STDERR
The standard error shall be used only for diagnostic messages.
OUTPUT FILES
EXTENDED DESCRIPTION
EXIT STATUS
The following exit values shall be returned: 0 All the specified directories were created successfully or the -p option was specified and all the specified directories now exist. >0 An error occurred.
CONSEQUENCES OF ERRORS
Default. The following sections are informative.
APPLICATION USAGE
The default file mode for directories is a= rwx (777 on most systems) with selected permissions removed in accordance with the file mode creation mask. For intermediate pathname components created by mkdir, the mode is the default modified by u+ wx so that the subdirectories can always be created regardless of the file mode creation mask; if different ultimate permissions are desired for the intermediate directories, they can be changed afterwards with chmod. Note that some of the requested directories may have been created even if an error occurs.
EXAMPLES
RATIONALE
The System V -m option was included to control the file mode. The System V -p option was included to create any needed intermediate directories and to complement the functionality provided by rmdir for removing directories in the path prefix as they become empty. Because no error is produced if any path component already exists, the -p option is also useful to ensure that a particular directory exists. The functionality of mkdir is described substantially through a reference to the mkdir() function in the System Interfaces volume of IEEE Std 1003.1-2001. For example, by default, the mode of the directory is affected by the file mode creation mask in accordance with the specified behavior of the mkdir() function. In this way, there is less duplication of effort required for describing details of the directory creation.
FUTURE DIRECTIONS
SEE ALSO
chmod() , rm , rmdir() , umask() , the System Interfaces volume of IEEE Std 1003.1-2001, mkdir()
Linux mkdir with chmod
The mkdir command creates the named directories in mode 777 (possibly altered by the file mode creation mask umask (1)).
Standard entries in a directory (for instance, the files » . «, for the directory itself, and » .. «, for its parent) are made automatically. mkdir cannot create these entries by name. Creation of a directory requires write permission in the parent directory.
The owner-ID and group-ID of the new directories are set to the process’s effective user-ID and group-ID, respectively. mkdir calls the mkdir (2) system call.
setgid and mkdir
To change the setgid bit on a newly created directory, you must use chmod g+s or chmod g-s after executing mkdir .
The setgid bit setting is inherited from the parent directory.
ksh93
The mkdir built-in in ksh93 is associated with the /bin and /usr/bin paths. It is invoked when mkdir is executed without a pathname prefix and the pathname search finds a /bin/mkdir or /usr/bin/mkdir executable. mkdir creates one or more directories. By default, the mode of created directories is a=rwx minus the bits set in umask (1).
OPTIONS
/usr/bin/mkdir
The following options are supported by /usr/bin/mkdir :
-m mode This option allows users to specify the mode to be used for new directories. Choices for modes can be found in chmod (1).
-p With this option, mkdir creates dir by creating all the non-existing parent directories first. The mode given to intermediate directories is the difference between 777 and the bits set in the file mode creation mask. The difference, however, must be at least 300 (write and execute permission for the user).
ksh93
The following options are supported by the mkdir built-in in ksh93 :
-m mode
—mode= mode Set the mode of created directories to mode . mode is symbolic or octal mode as in chmod (1). Relative modes assume an initial mode of a=rwx .
-p
—parents Create any missing intermediate pathname components. For each dir operand that does not name an existing directory, effects equivalent to those caused by the following command shall occur:
mkdir -p -m $(umask -S),u+wx \ $(dirname dir) && mkdir [-m mode] dir
Where the -m mode option represents that option supplied to the original invocation of mkdir , if any. Each dir operand that names an existing directory is ignored without error.
OPERANDS
The following operand is supported:
dir A path name of a directory to be created.
USAGE
See largefile (5) for the description of the behavior of mkdir when encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
EXAMPLES
creates the subdirectory structure ltr/jd/jan .
ENVIRONMENT VARIABLES
See environ (5) for descriptions of the following environment variables that affect the execution of mkdir : LANG , LC_ALL , LC_CTYPE , LC_MESSAGES , and NLSPATH .
EXIT STATUS
The following exit values are returned:
0 All the specified directories were created successfully or the -p option was specified and all the specified directories now exist.
ATTRIBUTES
See attributes (5) for descriptions of the following attributes:
/usr/bin/mkdir
|
ksh93
|
The ksh93 built-in binding to /bin and /usr/bin is Volatile. The built-in interfaces are Uncommitted.
SEE ALSO
Index