mkdir Command in Linux with Examples
mkdir Command
mkdir command is used to create a new directory in Linux machine. This command is default shell command in Linux.
Syntax
mkdir [option] directory name
Make directory in current working directory
mkdir command is used to make new directory in current working directory.
Output
[user1@Linuxhelp]$ mkdir redhat [user1@Linuxhelp]$ pwd /home/user1/ [user1@Linuxhelp]$ ls -l total 4 drwxr-xr-x. 2 root root 4096 Jan 22 15:48 redhat
Make two or more directory in single command
mkdir command is used to make two or more directories using a single command.
Example
[user1@Linuxhelp]$ mkdir red1 red2
Output
[user1@Linuxhelp]$ ls -l
total 12
drwxr-xr-x. 2 root root 4096 Jan 22 16:07 red1
drwxr-xr-x. 2 root root 4096 Jan 22 16:07 red2
Make Collaborative Directory
mkdir command is used to make a nested directory.
Output
[user1@Linuxhelp]$ mkdir -p red3/red4/red5 [user1@Linuxhelp]$ ls -l total 16 drwxr-xr-x. 2 root root 4096 Jan 22 16:07 red1 drwxr-xr-x. 2 root root 4096 Jan 22 16:07 red2 drwxr-xr-x. 3 root root 4096 Jan 22 16:26 red3 drwxr-xr-x. 2 root root 4096 Jan 22 15:48 redhat [user1@Linuxhelp]$ cd red3 [user1@Linuxhelp]$ ls -l total 4 drwxr-xr-x. 3 root root 4096 Jan 22 16:26 red4 [user1@Linuxhelp]$ cd red4 [user1@Linuxhelp]$ ls -l total 4 drwxr-xr-x. 2 root root 4096 Jan 22 16:26 red5
Make Directory with Confirm Message
mkdir command will make new directory with confirm message
Output
[user1@Linuxhelp]$ mkdir -v red4 mkdir: created directory `red4' [user1@Linuxhelp]$ ls -l total 20 drwxr-xr-x. 2 root root 4096 Jan 22 16:07 red1 drwxr-xr-x. 2 root root 4096 Jan 22 16:07 red2 drwxr-xr-x. 3 root root 4096 Jan 22 16:26 red3 drwxr-xr-x. 2 root root 4096 Jan 22 16:41 red4 drwxr-xr-x. 2 root root 4096 Jan 22 15:48 redhat
Make directory with file permission
mkdir command will make directory with 444 permission
Output
[user1@Linuxhelp]$ mkdir -m 444 red5
[user1@Linuxhelp]$ ls -l
total 24
drwxr-xr-x. 2 root root 4096 Jan 22 16:07 red1
drwxr-xr-x. 2 root root 4096 Jan 22 16:07 red2
drwxr-xr-x. 3 root root 4096 Jan 22 16:26 red3
drwxr-xr-x. 2 root root 4096 Jan 22 16:41 red4
dr--r--r--. 2 root root 4096 Jan 22 16:47 red5
drwxr-xr-x. 2 root root 4096 Jan 22 15:48 redhat
Make directory in the specified path from the current directory
Here, the new directory is created from the current location to the desired location.
Output
[user1@linuxhelp Desktop]$ mkdir /home/user1/Desktop/redhat7
[user1@linuxhelp Desktop]$ ls &ndash l
total 24
drwxr-xr-x. 2 user1 user1 4096 Jan 22 16:07 redhat1
drwxr-xr-x. 2 user1 user1 4096 Jan 22 16:07 redhat2
drwxr-xr-x. 3 user1 user1 4096 Jan 22 16:26 redhat3
drwxr-xr-x. 2 user1 user1 4096 Jan 22 15:48 redhat6
dr--r--r--. 2 user1 user1 4096 Jan 22 15:53 redhat8
drwxr-xr-x. 2 user1 user1 4096 Jan 22 15:48 redhat7
Tag :
mkdir
Q
How to set SELinux context for any directory while creating itself using the "mkdir" command?
A
You can use the option of "-Z" with "mkdir" command to set SELinux context for any directory while creating itself using the "mkdir" command. mkdir -Z selinux_label /dir/".
Q
How Do I know the entire information and options to be available for "mkdir" command?
A
You can use the option of "--help" and "man" page of "mkdir" command to know the entire information and options to be available for "mkdir" command. For Ex: "mkdir --help" (or) "man mkdir".
Q
How Do I set the permission of the directory while creation process itself?
A
Use the option of "-m" with mkdir command to set the permission of the directory while creation process itself. For Syntax: "mkdir -m 775 /dir/".
Q
How to print the message of an action to be while executing the "mkdir" command?
A
You can use the option of "-v" with "mkdir" command to print the message of an action to be while executing the "mkdir" command. For Ex: "mkdir -v /dir/".
Q
How do I create multiple inherited directory without any parent directory using the "mkdir" command?
A
You can use the option of "-p" with "mkdir" command to create multiple inherited directory without any parent directory using the "mkdir" command. For Synta: "mkdir -p /dir1/dir2/dir3/dir4/".