8 Useful mkdir Command Examples for Linux Users

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.

Linux-mkdir-command-examples

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:

mkdir-command-options

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,

[root@linuxbuzz ~]# mkdir sysadmin

Use below ls command to verify whether directory is created successfully or not,

[root@linuxbuzz ~]# ls -ld sysadmin
drwxr-xr-x. 2 root root 6 Aug 18 00:37 sysadmin
[root@linuxbuzz ~]#

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,

[root@linuxbuzz ~]# mkdir /tmp/backup
[root@linuxbuzz ~]# ls -ld /tmp/backup/
drwxr-xr-x. 2 root root 6 Aug 18 00:45 /tmp/backup/
[root@linuxbuzz ~]#

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,

[root@linuxbuzz ~]# mkdir codedb sysdb backupdb
[root@linuxbuzz ~]#

If you want to create these directories in /tmp as well, use the below command,

[root@linuxbuzz ~]# mkdir /tmp/{codedb,sysdb,backupdb}
[root@linuxbuzz ~]#

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,

[root@linuxbuzz ~]# mkdir /mnt/sysadmins/engineers
mkdir: cannot create directory ‘/mnt/sysadmins/engineers’: No such file or directory
[root@linuxbuzz ~]#

So, if you want to create parent directory in case it not present then use “-p” option in mkdir command,

[root@linuxbuzz ~]# mkdir -p /mnt/sysadmins/engineers
[root@linuxbuzz ~]#

Use tree command to verify the directory hierarchy

[root@linuxbuzz ~]# tree /mnt/
/mnt/
└── sysadmins
    └── engineers
2 directories, 0 files
[root@linuxbuzz ~]#

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.

[root@linuxbuzz ~]# mkdir -p /opt/lab01/{expr1,expr2} /opt/lab02/{expr3,expr4}
[root@linuxbuzz ~]#

Use below tree command to verify the directory structure

[root@linuxbuzz ~]# tree /opt/
/opt/
├── lab01
│   ├── expr1
│   └── expr2
└── lab02
    ├── expr3
    └── expr4
6 directories, 0 files
[root@linuxbuzz ~]#

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

[root@linuxbuzz ~]# mkdir -m 700 /opt/shared
[root@linuxbuzz ~]#

Run the following “ls -ld” command to verify the directory permissions,

[root@linuxbuzz ~]# ls -ld /opt/shared/
drwx------. 2 root root 6 Aug 18 01:47 /opt/shared/
[root@linuxbuzz ~]#

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.

[root@linuxbuzz ~]#  mkdir -v /tmp/pkumar
mkdir: created directory ‘/tmp/pkumar’
[root@linuxbuzz ~]# 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’
[root@linuxbuzz ~]#

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

[root@linuxbuzz ~]# mkdir -Z /tmp/linuxbuzz
[root@linuxbuzz ~]# ls -Zd /tmp/linuxbuzz/
drwxr-xr-x. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/linuxbuzz/
[root@linuxbuzz ~]#

Apart from examples, if you want to check mkdir version then execute the below command,

[root@linuxbuzz ~]# 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.
[root@linuxbuzz ~]#

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

1 thought on “8 Useful mkdir Command Examples for Linux Users”

Leave a Comment

nine + twelve =