# mkdir

## Introduction

mkdir ( make directory ) is used to create directory(ies), if they don't exist already.

***

## Syntax

`mkdir [OPTION]... DIRECTORY...`

***

## Important Flags

| Flag                    | Description                                                              |
| ----------------------- | ------------------------------------------------------------------------ |
| `-p`                    | Creates parent directories if they don't exist.                          |
| `-m a=[rwx] [dir_name]` | Sets the file modes, i.e., permissions, for the new directory.           |
| `-v`                    | Displays a message for each created directory.                           |
| `--version`             | Displays the version number and information about the license and exits. |
| `-Z`                    | Sets the SELinux security context for directories.                       |
| `-help`                 | Displays help with information about mkdir options.                      |

***

## Examples

<table><thead><tr><th>Command</th><th>Description</th></tr></thead><tbody><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir Linux
</code></pre></td><td>To create a single directory named Linux</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir "My Documents"
</code></pre></td><td>To create a directory with spaces in its name, enclose the entire directory name in quotes.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir /tmp/example
</code></pre></td><td>To create a directory or directories in a specific location other than your current working directory, specify the full directory path.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir my_folder/sub_folder
</code></pre></td><td>This command creates a directory structure with “my_folder” as the parent directory and “sub_folder” as its subdirectory.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir dir1 dir2 dir3
</code></pre></td><td>To create multiple directories at once, list the directory names separated by a space.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir {test1,test2,test3}
</code></pre></td><td>Use mkdir with curly brackets to create multiple directories without spaces.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir dir{1..15}
</code></pre></td><td>Create a batch of directories starting with the same pattern, from dir1 to dir15.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir $USER
</code></pre></td><td>mkdir allows you to use environment variables in directory names. This example creates a directory with the current user being the directory name.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir –p Linux/dirtest1/dirtest2
</code></pre></td><td>To build a structure with multiple subdirectories. This ensures that mkdir adds any missing parent directories in the process. If the directories exist, no error is specified.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir -p first/second/third
</code></pre></td><td>If the first and second directories do not exist, due to the -p option, mkdir will create these directories. If the -p option is not specified, an error is returned.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir –m 777 DirM
</code></pre></td><td>To add all permissions for all users when creating a directory, specify the -m option with 777.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir -m a=rwx [directories]
</code></pre></td><td>This option is used to set the file modes, i.e. permissions, for the created directories.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir -m 755 public
</code></pre></td><td>Use the ‘-m’ option to set specific permissions for a directory. This command sets read, write, and execute permissions for the owner, and read and execute permissions for others.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir -v [directories]
</code></pre></td><td>To see the details of the mkdir process. It displays a message for every directory created.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir --help
</code></pre></td><td>It displays help-related information and exits.</td></tr><tr><td><pre class="language-bash" data-overflow="wrap"><code class="lang-bash">mkdir --version
</code></pre></td><td>It displays the version number, some information regarding the license and exits.</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://thamizhiniyancs.gitbook.io/cheatsheets/linux/manipulating-files-directories/mkdir.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
