Linux system allows you to create directories (Folder) form both command line interface and as well graphical interface. The mkdir is an Unix like operating system command used to create new directory(ies).
You can create multiple sub-directories with one mkdir command.
In this tutorial i will instruct you how to use mkdir command to create new directory and the use of different options with handful of exmaples.
The general syntax of mkdir command is as below.
$ mkdir [option] Directory...
You can give one or more directory name as argument in this command.
There are various options in mkdir command. Let us see the table and description of those options and how it is helpful while invoked with mkdir.
|-m (mode)||set file mode (as in chmod), not a=rwx – umask|
|-v (verbose)||Prints a message what has been done while creating a directory|
|-p (parents)||no error if existing, make parent directories as needed|
|-z (context)||set the SELinux security context of each created directory to CTX|
How to create a directory ?
To create a new directory the command will be as below. For example am creating a directory named Folder, mkdir will take Folder as an argument.
$ mkdir Folder
Once you invoke the command mkdir will create a directory silently. And you check if the directory is created or not by ls command.
But if you run the above command with -v (verbose) option the output will be as below, and it will notify what has been happened when you invoked the command.
$ mkdir -v Folder
Output: mkdir: created directory 'Folder'
Sometimes, the system denies to create directory.
$ mkdir Folder Output: mkdir: Failed to make directory "Folder"; Permission denied
There may be several causes which refusing to create the directory. And the reasons may be
- The directory ‘Folder’ may be exists
- There may be an ordinary file with the same name exists in the current working directory.
- The user does not have permission set for the current directory to create files and directories.
- If you are trying to create directory in /bin , /etc or any other directory that hoses in Linux file system.
How to create a directory tree ?
The Linux system allows you create directory trees with just one invocation of one command.
For example : If you invoke the below command it will create a directory tree.
$ mkdir X X/X1 X/X2
This command creates 3 sub-directories, X and two more sub-directories under X i.e. X1 and X2 .But the order of specifying the argument is important. You can not create child directory ( X1 and X2) without creating parent directory X.
For example: You can not invoke the below command.
$ mkdir X/X1 X/X2 X
Output: mkdir: Failed to make directory "X/X1" ; No such file or directory mkdir: Failed to make directory "X/X2" ; No such file or directory
Important: Even though running the above command will not create the directory X1 and X2 but it will crate the directory X.
How to create Multiple directory ?
The mkdir utility allows you to create multiple directories. You can specify the directory names as argument with mkdir command separated by space.
For example: To create 3 sub-directories named X1, X2, X3, run the below command.
$ mkdir X1 X2 X3
So far, we have covered the basic understanding on mkdir command. By following this tutorial you will be able to work with mkdir command very easily. For more info on this command you can type man mkdir on your terminal or refer to man makdir page.