Introduction to Mkdir Command in Linux
We know that Linux system is made up of files and directories. There may be a need for directories to be created in our system which is critical for some operations. So we need to create such directories or folders at our will. In Linux there is a command called mkdir which is used to create directories or folders in the operating system. In this article we will discuss about how to use the mkdir command with various command line options.
Command Description: The mkdir command is used to create directories in our Linux Operating System. Every directory that needs to be created will also contain some files or subdirectories that a user wants. With the help of mkdir command we can create multiple directories with setting the directories with permissions. Without permission we cannot create directories and we will face with an error saying “permission denied”. The directories that must be created needed to be named so that the mkdir commands creates that directory when there is no directory already exist under that name. But if the name already exists, the command will not create the directory. Multiple directories can be named to create and also sub-directories can be named in the particular order will also be created using the mkdir command.
The syntax of the Mkdir Command in Linux is given below.
mkdir [OPTION]... DIRECTORY...
This command is the basic command to create the directory. We just have to name the directory we want to create.If we want to create multiple directories, we can use following command.
mkdir Doc media pics
We need to give the name of the directories with a space in between to create multiple directories.
Options of Mkdir Command in Linux
There are plenty of options that can be used with the mkdir command. Let us discuss them one by one.
mkdir [-m=mode] [-p] [-v] [-Z=context] directory [directory ...]
There are plenty of options that can be used with the mkdir command.
Let us discuss them one by one:
-m option: This -m option is used to set the mode of the directory i.e. permissions such as read, write and execute for the created directory.
$ mkdir -m a=rwx [directories]
-p option: This option enables the user to create parent directories as per the requirement. If the directories exist already, no error will be displayed.
$ mkdir -p [directories]
-v, –verbose: This option displays a verbose information of each directory that is created.
$ mkdir -v [directories]
–version: This option will print the information of the version and exit.
$ mkdir –version
–help: This option will display the information of the mkdir command. It will list the syntax of the command and also the options used in mkdir command and a brief description of each option.
$ mkdir --help
-Z option: This option is used to set defaul SELinux rules on a particular directory at the time of creation.
$ mkdir -Z [directories]
Examples to Implement Mkdir Command in Linux
Let us discuss a few examples to get a better understanding:
$ mkdir pics
The above command will help to create the directory called pics in the user’s current directory.
$ mkdir ~pics
The above command will create the directory called pics in the user’s home directory
To create multiple directories at once use the following command.
mkdir sample1 sample2 sample3
The above command will create the directories sample1 sample2 and sample3 if they do not exist.
The above command will create the directory named b inside the directory a. If the directory a does not exist then it will display an error message. Using the –p option will create the parent directory if it does not exist.
rmdir –p a/b
It is same as the command shown in example 3. mkdir command will create the directory a if it does not exist and create a directory called b inside the directory a.
Using Mkdir Command in Linux With Options
Now we shall discuss how to use mkdir command along with the options.
- How To Create Parent Directories Using Mkdir Command?
$ mkdir -p test/testdir
The above command will create the directory named testdir inside the directory test. If the directory test does not exist already due to the presence of –p option the directory will be created. To check the created directory use the option -v.
- How to Get Help For Mkdir Command?
To get help about the mkdir command use the following command.
$ mkdir --help
You can also use the man command to display the manual pages of the rmdir command.
$ man mkdir
- How to Display the Version Information of Mkdir Command?
To display the version information, use the –version option along with mkdir command as shown below.
$ mkdir --version
- How to Set Permissions for the Created Directory?
You can set permissions for the created directory using the –m command line option. To set the permission, use the command shown below.
$ mkdir –m a=rwx sample1
The above command will create a directory called sample1 with the read, write and execute permissions available to all the users accessing the directory. If you want to give only read access to the user you can set the value a=r and if you want to give both read and write access to the user you can set the value a=rw.
- How to Create Directories in Verbose Mode?
We can create new directories in verbose mode by using the –v option. When you create a new directory using this option it will produce the following verbose output in the screen.
Advantages of the Mkdir Command in Linux
- It can create multiple directories at once.
- The mkdir command is simple and easy to use both by beginners and experts.
In this article, we have discussed about the mkdir command and how to use mkdir using with the various command line options. This command is simple and easy to use both by newcomers and experts. I hope this article is useful. Thank you.
This is a guide to Mkdir Command in Linux. Here we discuss the introduction to Mkdir Command in Linux, syntax, examples, and options with explanation. You can also go through our other related articles to learn more –