
mkdir Command in Linux
mkdir is a command used in Linux to create new directories within the filesystem. This command is pretty useful in case someone wants to organize files and folders, especially in an environment without a graphical user interface (GUI).
mkdir is a simple and efficient command that is a perfect choice for both small-scale and large-scale directory creation tasks. It can create single or multiple directories at once, and even allows for the creation of parent directories with a single command.
Table of Contents
Here is a comprehensive guide to the options available with the mkdir command −
Syntax of mkdir Command
Here's the standard syntax for the mkdir command on Linux −
mkdir [options] directory_name
Where,
- options can comprise multiple flags to change the way the command functions.
- directory_name indicates the name of the directory to be created.
mkdir Command Options
Options | Description |
---|---|
-p, --parents | Creates parent directories if they don't already exist. |
-v, --verbose | Displays a message for each directory that is created. |
-m, --mode | Sets the permissions for the new directory using a mode value. |
--help | Shows help information about the command and its options. |
--version | Displays the version information of the mkdir command. |
-Z | Sets the SELinux security context of each created directory to the default type. |
--context [=CTX] | Sets the SELinux or SMACK security context to the specified value (CTX). |
Examples of mkdir Command in Linux
Take a look at these examples to learn how the mkdir command works in Linux −
- Creating a Single Directory
- Creating Multiple Directories Simultaneously
- Creating Nested Directories
- Setting Specific Permission for a New Directory
- Creating a Directory with Detailed Output
- Applying SELinux Security Context
Creating a Single Directory
To set up a new directory named mydir, use this command −
mkdir mydir
Running this command will result in a fresh directory called mydir appearing in your current location. This is a fundamental operation for organizing your files.

Creating Multiple Directories Simultaneously
If you need to create several directories at once, such as dir1, dir2, and dir3, execute −
mkdir dir1 dir2 dir3
This command will generate three separate directories, named dir1, dir2, and dir3, in one go. It's a quick and efficient way to establish multiple folders without repeating the command.

Creating Nested Directories
To construct a nested directory structure and ensure all parent directories are established as required, use −
mkdir -p /path/to/newdir
Here, the -p option ensures that any missing parent directories along the specified path are created as needed. This is particularly useful when you want to build an entire directory tree.
Setting Specific Permissions for a New Directory
If you need to create a directory with defined permissions, such as read, write, and execute for the owner, and read and execute for others, use −
mkdir -m 755 mydir2
The -m option allows you to set the permissions for the new directory. In this case, 755 sets the directory to be fully accessible by the owner while others can only read and execute.

Creating a Directory with Detailed Output
To see detailed messages about each directory created, use the -v option −
mkdir -v mydir3
The -v (verbose) option makes the command print a confirmation message for each directory it creates. This can be handy for tracking your operations in scripts.

Applying SELinux Security Context
To assign the SELinux security context to the newly created directory, use −
mkdir -Z mydir4
The -Z option sets the SELinux security context for the directory, ensuring it complies with your system's security policies.

Conclusion
The mkdir command in Linux is an essential tool for creating new directories within the filesystem. It's simple, efficient, and versatile, making it perfect for organizing files and managing the directory structure. By learning its syntax and exploring various options, you can effectively use mkdir to enhance your file management practices.
Whether you're setting up nested directories, adjusting permissions, or setting SELinux contexts, mkdir provides a flexible and powerful solution for your needs.