Table of Contents
In this article, I will take you through 8 Most Popular mkdir command in Linux with Examples. mkdir command is used to create directories in Linux if it does not exists. It can be used to create multiple directories at different locations and can also be used to create directories recursively. You can also set the directory permission at the time of creation. I will go through all these details below and explain the concepts through examples.
mkdir Syntax
mkdir [OPTION]... DIRECTORY...
mkdir command in Linux
How to Install Angular CLI on RedHat/CentOS 7 in 9 Easy Steps
Example 1: Check mkdir command version
You can use --version
option to check mkdir
command version. As you can see from below output, current version is 8.22
.
[root@localhost ~]# 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.
--version :
output version information and exit
Example 2: Create a Directory
If you want to simply create a directory example under /home/centos
path, then you need to use mkdir /home/centos/example
command as shown below.
[root@localhost ~]# mkdir /home/centos/example
Once above command ran successfully you can check if the directory is created or not by running ls -lrtd /home/centos/example
command.
[root@localhost ~]# ls -lrtd /home/centos/example/ drwxr-xr-x 2 root root 6 Apr 8 12:51 /home/centos/example/
If directory already exists and you are again trying to create the same directory, then it will show below "File exists"
message.
[root@localhost ~]# mkdir -v /home/centos/example mkdir: cannot create directory ‘/home/centos/example’: File exists
Example 3: Create Multiple Directories
You can also create multiple different directories in one single command as shown below. Here we are creating example
, example1
and example2
directories under /home/centos
path. You can also create directory at different location using single mkdir command.
[root@localhost ~]# mkdir -v /home/centos/example /home/centos/example1 /home/centos/example2 mkdir: created directory ‘/home/centos/example’ mkdir: created directory ‘/home/centos/example1’ mkdir: created directory ‘/home/centos/example2’
Example 4: Create a Directory with Read Permission
By default when you create a directory permission will be assigned based on umask value. If you want to change that permission then you need to use -m
option with mkdir
command to set the permission value. In this example we are providing read only permission to example
directory.
[root@localhost ~]# mkdir -m=rrr /home/centos/example
-m :
Set Directory permission
You can verify the directory creation and its permission by using ls -lrtd /home/centos/example
command as shown below.
[root@localhost ~]# ls -lrtd /home/centos/example/ dr--r--r-- 2 root root 6 Apr 8 12:52 /home/centos/example/
Example 5: Make Directory as parent directory
Sometimes you need to create recursive directories in a single command. You can do that by using -p
option. Here we are creating example
directory and test1
directory under example
directory. If any of the below mentioned parent directory /home
or /home/centos
is missing, then it will be created using -p
option.
[root@localhost ~]# mkdir -p /home/centos/example/test1
-p :
no error if existing, make parent directories as needed
You can verify the directory creation by using ls -lrtd /home/centos/example/test1
command.
[root@localhost ~]# ls -lrtd /home/centos/example/test1 drwxr-xr-x 2 root root 6 Apr 8 12:55 /home/centos/example/test1
Example 6: Create a directory in verbose mode
Another important option that can be used with mkdir command is -v
option using which you can see the output of mkdir command. Usually when you run mkdir command to create a directory, you won't see anything in the output. You can only verify the success of directory creation later by checking that manually. So to avoid this you can always use -v
option to see the mkdir command output immediately.
[root@localhost ~]# mkdir -v /home/centos/example mkdir: created directory ‘/home/centos/example’
-v :
print a message for each created directory
You can verify the directory creation by using ls -lrtd /home/centos/example
command.
[root@localhost ~]# ls -lrtd /home/centos/example/ drwxr-xr-x 2 root root 6 Apr 8 12:57 /home/centos/example/
Example 7: Create directory with SELinux Context set to default type
If you want to create a directory with SELinux context set to default type then you need to use -Z
option with mkdir command as shown below.
[root@localhost ~]# mkdir -Zv /home/centos/example
mkdir: created directory ‘/home/centos/example’
-Z :
set SELinux security context of each created directory to the default type.
You can verify the directory creation by using ls -lrtd /home/centos/example
command.
[root@localhost ~]# ls -lrtd /home/centos/example/ drwxr-xr-x 2 root root 6 Apr 8 13:02 /home/centos/example/
Example 8: Check Other mkdir command Options
You can check other options that can be used with mkdir command using --help
as shown below.
[root@localhost ~]# mkdir --help Usage: mkdir [OPTION]... DIRECTORY... Create the DIRECTORY(ies), if they do not already exist. Mandatory arguments to long options are mandatory for short options too. -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask -p, --parents no error if existing, make parent directories as needed -v, --verbose print a message for each created directory -Z set SELinux security context of each created directory to the default type --context[=CTX] like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX --help display this help and exit --version output version information and exit GNU coreutils online help: <http://www.gnu.org/software/coreutils/> For complete documentation, run: info coreutils 'mkdir invocation'
26 Popular Rsync Examples for Linux Professionals
8 Popular Steps to Install Mono on RedHat/CentOS 7
Popular firewalld examples to open a port on RedHat/CentOS 7
Upgrade to CentOS 8 from CentOS 7 using 20 Easy Steps