mkdir (make directory)
is the basic command for Unix like systems for creating directory in file system. This is the frequently used command by the Unix/Linux system users. If you are are Linux system user, you must be aware about this command.
This tutorial described you to how to create directory on Unix/Linux based systems along with various available option. Let’s discuss about Linux mkdir command with useful examples.
Syntax
mkdir [OPTION]... DIRECTORY...
Create Directory in Linux
Use mkdir followed by the directory name to be created under the current directory.
mkdir mydir
You can also create directory at other location by providing complete path.
mkdir /opt/mydir
mkdir Command Examples
Here is some more useful examples of mkdir command on Unix/Linux systems.
- Create directory in current directory – Use the following command to create directory in present working directory. Just use mkdir command followed by the directory name to be created.
mkdir newdir
- Create directory at other location – You can also use absolute or related directory path to create a directory inside a other directory. Like:
The following command will create a directory named backup under the /opt/files directory:
mkdir /opt/files/backup
You can also use relative path while crating directory. For example, the belwo command will create directory under the files directory available in present working directory.
mkdir files/backup
- Creating multiple directory – You can also created multiple directories in a single command. Just pass all the directory names including path for directories at other locations.
mkdir dir1 dir2 /opt/dir3
- Create directory with parent directory – The default mkdir command will return error if any parent directory doesn’t exits. But you use “-p” option to instruct the mkdir command to create any parent directory if not exists.
mkdir -p /var/www/folder1/public
- Create directory with specific permissions – Use
-m
option to to set default permission to a directory during creation time. This is similar to create directory and change permission with chmod command.mkdir -m 644 dir1
mkdir -m a=rwx dir1
mkdir -m u=rw,g=r,o=r dir1
- Create multiple directories at specific location – This is very lesser known option for creating multiple directories on different location with single command. The following command will create directories named one, two, three under the mydir directory.
mkdir -p mydir/{one|two|three}
Conclusion
In this tutorial, you have leaned about mkdir command in Linux with examples. If you have any other useful examples, please do share in comments and we will include them in this tutorial.
Leave a Reply