As the name suggests, mkdir command is used to create or make directories in Linux like systems. For Linux beginners, mkdir is one of the important command, they should understand the basic usage and its functionalities. Apart from directory creation, mkdir command can also set permission on directories at the time of creation.
In Linux terminology directory or folder both are same, in this article we will demonstrate 8 useful mkdir command practical examples.
Syntax: # mkdir <options> <dir_name>
Some of the mkdir options are described below:
Let’s jump into the examples now.
Example:1) Create an empty directory
To create an empty directory in Linux like systems type mkdir followed by the directory name, let’s assume we want to create “sysadmin” directory,
[[email protected] ~]# mkdir sysadmin
Use below ls command to verify whether directory is created successfully or not,
[[email protected] ~]# ls -ld sysadmin drwxr-xr-x. 2 root root 6 Aug 18 00:37 sysadmin [[email protected] ~]#
Note: In case you don’t specify the path of directory then it will be created under your present working directory
Example:2) Creating a directory in specific folder
Let’s assume we want to create a directory named “backup” under /tmp , execute the below command,
[[email protected] ~]# mkdir /tmp/backup [[email protected] ~]# ls -ld /tmp/backup/ drwxr-xr-x. 2 root root 6 Aug 18 00:45 /tmp/backup/ [[email protected] ~]#
Example:3) Creating multiple directories in one go
Let’s suppose we want to create multiple directories in one go in our current working directory,
[roo[email protected] ~]# mkdir codedb sysdb backupdb [[email protected] ~]#
If you want to create these directories in /tmp as well, use the below command,
[[email protected] ~]# mkdir /tmp/{codedb,sysdb,backupdb} [[email protected] ~]#
Example:4) Creating parent directory (mkdir -p)
There are some scenarios where we want to create parent and its child directories at time of their creation. Let’s assume we want to create a parent directory under /mnt with name “sysadmins” and under sysadmins we want to create “engineers” directory.
if we try the below mkdir command, we will get the error,
[[email protected] ~]# mkdir /mnt/sysadmins/engineers mkdir: cannot create directory ‘/mnt/sysadmins/engineers’: No such file or directory [[email protected] ~]#
So, if you want to create parent directory in case it not present then use “-p” option in mkdir command,
[[email protected] ~]# mkdir -p /mnt/sysadmins/engineers [[email protected] ~]#
Use tree command to verify the directory hierarchy
[[email protected] ~]# tree /mnt/ /mnt/ └── sysadmins └── engineers 2 directories, 0 files [[email protected] ~]#
Example:5) Creating Complex directory structure
There can be some requirements in our day to day operations task to create complex directory structure, so complex directory structure can be created using mkdir command, let’s suppose we want to create two parent directories named as “lab01” and “lab02” under /opt folders and their child directories as expr1, expr2 and expr3, expr4 respectively. Execute the beneath mkdir command to create above said directory hierarchy under /opt.
[[email protected] ~]# mkdir -p /opt/lab01/{expr1,expr2} /opt/lab02/{expr3,expr4} [[email protected] ~]#
Use below tree command to verify the directory structure
[[email protected] ~]# tree /opt/ /opt/ ├── lab01 │ ├── expr1 │ └── expr2 └── lab02 ├── expr3 └── expr4 6 directories, 0 files [[email protected] ~]#
Example:6) Set permissions while creating directory (mkdir -m)
Using -m option in mkdir command we can set the permissions during the creation of directory.
Let’s suppose we want to create a directory named “shared” under /opt and set the permissions as 700, run the below mkdir command
[[email protected] ~]# mkdir -m 700 /opt/shared [[email protected] ~]#
Run the following “ls -ld” command to verify the directory permissions,
[[email protected] ~]# ls -ld /opt/shared/ drwx------. 2 root root 6 Aug 18 01:47 /opt/shared/ [[email protected] ~]#
Note: By default directories are getting the permission based on umask.
Example:7) Creating Directories in verbose mode (mkdir -v)
Using “-v” option in mkdir command we can create directories in verbose mode, it means we will get verbose output on screen during the directory creation, example is shown below.
[[email protected] ~]# mkdir -v /tmp/pkumar mkdir: created directory ‘/tmp/pkumar’ [[email protected] ~]# mkdir -v /tmp/pkumar/{dir1,dir2,dir3} mkdir: created directory ‘/tmp/pkumar/dir1’ mkdir: created directory ‘/tmp/pkumar/dir2’ mkdir: created directory ‘/tmp/pkumar/dir3’ [[email protected] ~]#
Example:8) Set default SELinux rules on directory at the time of creation (mkdir -z)
Using “-Z” option in mkdir command, we can set default selinux rules on the directory at time of creation, example is show below
[[email protected] ~]# mkdir -Z /tmp/linuxbuzz [[email protected] ~]# ls -Zd /tmp/linuxbuzz/ drwxr-xr-x. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/linuxbuzz/ [[email protected] ~]#
Apart from examples, if you want to check mkdir version then execute the below command,
[[email protected] ~]# mkdir --version mkdir (GNU coreutils) 8.22 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by David MacKenzie. [[email protected] ~]#
That’s all from this tutorial, for more details on mkdir command please refer its man page (man mkdir)
Also Read : Learn IP Command to Manage Networking on Linux
Hi
Good and very useful information about mkdir command. Every option is explained well and easy to understand.