![]() |
VOOZH | about |
The mkdir command in Linux stands for βmake directoryβ and is used to create new folders quickly and efficiently from the terminal.
To create a single directory, use the following syntax:
For Example:
If we want to create a directory name "jayesh_gfg".
Syntax:
mkdir jayesh_gfgIf we want to create a directory name "geeksforgeeks" and see verbose at same time. You can enter your directory_name.
Syntax:
mkdir -v geeksforgeeks
Here we have used `ls` command to display all files and directories.
To create multiple directories at once, you can specify multiple directory names separated by spaces:
For Example:
If we want to create a directory name "jayesh_gfg_1, jayesh_gfg_2, jayesh_gfg_3".
Syntax:
mkdir jayesh_gfg_1 jayesh_gfg_2 jayesh_gfg_3Here we have used `ls` command to display all files and directories.
If you encounter a "permission denied" error while creating a directory, you may not have permission to create directories in that location. To resolve this you can give root access to the user by using "sudo" command.
For Example:
If we want to create a directory name "geeksforgeek" with "sudo" permission. you can replace "geeksforgeek" directory_name with your directory_name. While using this command it may ask you to enter the password of root.
Syntax:
sudo mkdir geeksforgeekThe 'mkdir' command also supports absolute and relative paths. For example:
mkdir /path/to/directoryThis command creates a directory named "directory" at the specified absolute path.
mkdir my_folder/sub_folderThis command creates a directory structure with "my_folder" as the parent directory and "sub_folder" as its subdirectory.
mkdir [options...] [directory_name]Here, replace [directory_name] with the desired name of the directory you want to create. Let's delve into the functionality of the 'mkdir' command with various examples.
It displays help-related information and exits.
Syntax:
mkdir --help
It displays the version number, some information regarding the license and exits.
Syntax:
mkdir --version
It displays a message for every directory created.
Syntax:
mkdir -v [directories]
A flag which enables the command to create parent directories as necessary. If the directories exist, no error is specified.
Syntax:
mkdir -p [directories]
Suppose you execute the following command -
mkdir -p first/second/third
If the first and second directories do not exist, due to the -p option, mkdir will create these directories for us. If we do not specify the -p option, and request the creation of directories, where parent directory doesn't exist, we will get the following output -
If we specify the -p option, the directories will be created, and no error will be reported. Following is the output of one such execution. We've also provided the -v option, so that we can see it in action.
This option is used to set the file modes, i.e. permissions, etc. for the created directories. The syntax of the mode is the same as the chmod command.
Syntax:
mkdir -m a=rwx [directories]
The above syntax specifies that the directories created give access to all the users to read from, write to and execute the contents of the created directories. You can use 'a=r' to only allow all the users to read from the directories and so on.
| Option | Description |
|---|---|
--help | Shows help info for using mkdir. |
--version | Displays the installed version of mkdir. |
-v / --verbose | Prints messages for each directory created. |
-p | Creates parent directories if they donβt exist (no error if they already exist). |
-m | Sets permissions for new directories (e.g., -m 755). |