How to Create a Directory & Subdirectory in Linux
In this article we are learning about the creating and managing Directories & Subdirectories in the Linux operating system. With the help of “mkdir” command we are creating a directories. Mkdir is stands for “make directory”.
You can type mkdir directly from your console to use it
By default, running mkdir without any parameter will create directory under the current directory.
Here’s a sample of it
From screenshot above, we created directory called rules. When we run mkdir command, we are in /home/myuser directory. So then the new directory, which is rules, is created under /home/myuser 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 is already exist, then Linux will telling us that Linux can’t cretate it.
Create multiple directories
We can also create multiple directories at the same time. Let say we want to create directories named Blue, Black and White. Then the syntax will be like this :
# mkdir Blue Black White
Add directory include its sub-directory
When you want to created a include its sub-directory, 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
Set access privilege
Using -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.
Print message a message for each created directory
If we want, we can use -v parameter to do this. Here’s an example.
# mkdir -v Blue Black White