Let's check how to create a directory in the Linux system. On Linux, we can use 'mkdir' command. Mkdir is short for “make directory”. All most all Linux distribution like Ubuntu, RHEL, Debian and Centos uses the same command.
In this tutorial, I will show you 5 usage mkdir command to create directory on a Linux and Unix like operating system.
1) mkdir command
You can type mkdir directly from your console to use it.
By default, running mkdir without any parameter will create a directory under the current directory. Here’s a sample of it:
From the screenshot above, we created a directory called 'office'. When we run mkdir command, we are in '/home/pungki' directory. So then the new directory, which is office, is created under /home/pungki directory. If we put an exact location - for example : '/usr/local' - , then Linux will create a directory under '/usr/local' directory.
When Linux found that the directory which suppose to be created already exists, then the tool will tell us that it can’t create it.
Another pre-requisite for creating a directory that you must have access to the location where the directory wants to be created. When you don’t have it then mkdir will report an error.
2) Create multiple directories
We can also create multiple directories at the same time. Let say we want to create directories named 'ubuntu', 'redhat' and 'slackware'. Then the syntax will be like this :
$ mkdir ubuntu redhat slackware
3) Add directory include its sub-directory
When you want to create sub-directories , you will need to use
-p parameter. This parameter will create parent directory first, if mkdir cannot find it. Let say we want to create directory named 'letter' and directory named 'important' under directory letter. Then the syntax will be like this:
$ mkdir -p letter/important
4) Set access privilege
-m parameter, we can also set the access privilege for the new directory on-the-fly. Here’s an example.
$ mkdir -m=r-- letter
The above command will create a directory named letter and give access privilege read-only for the directory owner, directory group owner and anybody.
5) Print message for each created directory
-v option will print message on console for each created directory. Check the following example.
$ mkdir -v ubuntu redhat slackware
6) Create Directory with Date
Do you know that you can create a directory name with date? This is mostly useful in shell scripts when you require to create backup folders with date.
$ mkdir "$(date +"%d-%m-%Y")"
Output $ mkdir "$(date +"%d-%m-%Y")" $ ls 03-01-2019 $