In Linux, the ‘mkdir’ command is like a magic wand for creating folders super easily. ‘mkdir’ stands for “make directory,” and it helps you organize your computer stuff by creating folders with just one command. Whether you’re making one folder or a bunch of them in a row, ‘mkdir’ is there to help you keep things neat and tidy on your computer. In this guide, we’ll talk about how to use ‘mkdir,’ what words to type, and some cool tricks to make your folders just the way you want in Linux.
This command can create multiple directories at once as well as set the permissions for the directories. It is important to note that the user executing this command must have enough permission to create a directory in the parent directory, or he/she may receive a ‘permission denied’ error.
Table of Content
- Syntax of `mkdir` Command in Linux
- Options and their Practical Implementation in mkdir
- Examples on How to Create Directory in Linux by Using`mkdir` command
- How to Create Directory in Linux – FAQs
Syntax of `mkdir` Command in Linux
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.
Options and their Practical Implementation in mkdir
Options | Description |
---|---|
–help | Displays help-related information for the mkdir command and exits. Use this option to get assistance on how to use the command and its various features. |
–version | Displays the version number and additional information about the license for mkdir, providing details about the software version installed. Use this option to check the installed mkdir version. |
-v or –verbose | Enables verbose mode, displaying a message for every directory created. When used with the [directories] argument, it shows the names of the directories being created. |
-p | A flag that allows the creation of parent directories as necessary. If the specified directories already exist, no error is reported. Useful for creating a directory hierarchy without errors. |
-m | Sets file modes or permissions for the created directories. The syntax follows that of the chmod command. Use this option to specify permissions, such as read, write, and execute, for the new directories. |
1) `–help` Option in `mkdir`Command in Linux
It displays help-related information and exits.
Syntax:
mkdir --help
mkdir –help
2) `–version` Option in `mkdir`Command in Linux
It displays the version number, some information regarding the license and exits.
Syntax:
mkdir --version
mkdir –version
3) `-v` or `–verbose` Option in to Create Directory in Linux
It displays a message for every directory created.
Syntax:
mkdir -v [directories]
mkdir -v [directories]
Here we have used `ls` command to display all files and directories.
As we can see we have created tow directory with “names = jayeshghg_1 and jayeshgfg_2”, replace these names with the directory name you want.
4) `-p` Option to Create Directory in Linux
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 –
mkdir first/second/third
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.
-p option
5) `-m` Option to Create Directory in Linux
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.
mkdir -m a=rwx [directories]
Examples on How to Create Directory in Linux by Using`mkdir` command
1) How to create a directory in Linux using `mkdir` command?
To create a single directory, use the following syntax:
Syntax:
mkdir [directorie_name]
For Example:
If we want to create a directory name “jayesh_gfg”.
Syntax:
mkdir jayesh_gfg
This command creates a directory named “jayesh_gfg” in the current location. You can replace “jayesh_gfg” with any name you prefer.
mkdir jayesh_gfg
Here we have used `ls` command to display all files and directories.
2) How to create a directory with verbose output using `mkdir` command?
Syntax:
mkdir -v [directory_name]
For Example:
If we want to create a directory name “geeksforgeeks” and see verbose at same time. You can enter your directory_name.
Syntax:
mkdir -v geeksforgeeks
mkdir -v geeksforgeeks
Here we have used `ls` command to display all files and directories.
3) How to create multiple directories in Linux using `mkdir` command?
To create multiple directories at once, you can specify multiple directory names separated by spaces:
Syntax:
mkdir [directorie_name_1] [directorie_name_1] [directorie_name_1] .......
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_3
This command creates three directories named “jayesh_gfg_1″, ” jayesh_gfg_2″ and “jayesh_gfg_3” in the current location.
mkdir jayesh_gfg_1 jayesh_gfg_2 jayesh_gfg_3
Here we have used `ls` command to display all files and directories.
4) How to resolve permission denied error in `mkdir` command?
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 geeksforgeek
sudo mkdir geeksforgeek
5) How to Create Directory Using Absolute and Relative Paths
The ‘mkdir’ command also supports absolute and relative paths. For example:
mkdir /path/to/directory
This command creates a directory named “directory” at the specified absolute path.
mkdir my_folder/sub_folder
This command creates a directory structure with “my_folder” as the parent directory and “sub_folder” as its subdirectory.
How to Create Directory in Linux – FAQs
How do I create a directory with spaces in its name using the ‘mkdir’ command in Linux?
If you want to create a directory with spaces, you can enclose the entire directory name in quotes.
For example:
mkdir "My Documents"
Can I create multiple directories at once with the ‘mkdir’ command?
Yes, you can create multiple directories simultaneously by specifying their names separated by spaces.
For instance:
mkdir dir1 dir2 dir3
What is the purpose of the ‘-p’ option in the ‘mkdir’ command?
The ‘-p’ option allows you to create a directory hierarchy, including parent directories that don’t exist. It prevents errors if the specified directories already exist.
Example:
mkdir -p project/docs/images
How can I set specific permissions for a directory using the ‘mkdir’ command?
You can use the ‘-m’ option to set permissions for the created directory.
For example:
mkdir -m 755 public
This command sets read, write, and execute permissions for the owner and read and execute permissions for others.
Is there a way to create a directory and its subdirectories in one go with ‘mkdir’ in Linux?
Yes, you can use a combination of the ‘-p’ option and specify the directory structure you want to create.
For instance:
mkdir -p parent/child/grandchild
This command creates the “parent” directory and its subdirectories “child” and “grandchild” in one go.
Conclusion
If you’re a Linux user, you’ll find that the mkdir command is incredibly useful for creating directories or folders quickly and efficiently. It provides a range of options that can help you customize the way you create directories, such as verbose output, setting permissions, and creating multiple directories simultaneously. However, it’s worth noting that you’ll need the appropriate permissions to create directories in specific locations, or else you may run into “permission denied” errors. Learning how to use the mkdir command effectively can significantly improve your ability to organize files and directories and boost your productivity on Linux.
Previous Article
How to Display Current Working Directory in Linux | pwd Command
Next Article
rm command in Linux with examples