How to Use the Linux Command mkdir and Its 2 Options

Let’s explore the Linux command mkdir, which is used to create directories (folders) in Linux. mkdir stands for “make directory” and is used to create new directories. In this article, we’ll cover the basic usage of the mkdir command, its main options, useful applications, and some precautions when using it.

Basic Usage of the Linux Command mkdir

The basic usage of the Linux command mkdir is very simple. In the terminal, you enter mkdir followed by the name of the directory you want to create.

mkdir directory_name
ShellScript

For example, if you want to create a directory named test, you would enter the following:

mkdir test
ShellScript

When this command is executed, a directory named test is created in the current working directory.

Figure 1. Linux Command mkdir: Creating the test directory
Figure 1. Linux Command mkdir: Creating the test directory

Main Options

The mkdir command supports various options. Let’s look at some of the commonly used options.

-p Option (parents): Create Parent Directories

The -p option means “create parent directories.” If the parent directory of the directory you want to create does not exist, you can use the -p option to create the parent directory along with the target directory in one command.

mkdir -p parent_directory/child_directory
ShellScript

For example, if you want to create the parent/child directory and the parent directory does not yet exist, you would enter:

mkdir -p parent/child
ShellScript

When this command is executed, the parent directory will be created if it does not exist, and the child directory will be created inside it.

Figure 2. Linux Command mkdir: Creating the parent and child directories at once
Figure 2. Linux Command mkdir: Creating the parent and child directories at once

-v Option (verbose): Display Detailed Information

The -v option stands for “verbose.” When you use this option, detailed information about the created directory is displayed when the command is executed.

mkdir -v directory_name
ShellScript

For example, to create a directory named verbose and display detailed information, you would enter:

mkdir -v verbose
ShellScript

When this command is executed, a message like “mkdir: created directory ‘verbose'” will be displayed, confirming that the verbose directory was created.

Figure 3. Linux Command mkdir: Using the -v option to verify results
Figure 3. Linux Command mkdir: Using the -v option to verify results

Useful Applications

Creating Multiple Directories at Once

Using the mkdir command, you can create multiple directories at once by separating the directory names with spaces.

mkdir directory1 directory2 directory3
ShellScript

For example, to create three directories named dir1, dir2, and dir3 at once, you would enter:

mkdir dir1 dir2 dir3
ShellScript

As shown in the following figure, using the -v option allows you to clearly see which directories were created.

Figure 4. Linux Command mkdir: Creating multiple directories at once
Figure 4. Linux Command mkdir: Creating multiple directories at once

Creating Directory Structures at Once

Using the previously mentioned -p option, you can create complex directory structures at once. This method allows you to create not only two-level directories like parent/child, but also deeper multi-level directories at once.

mkdir -p project/src/module
ShellScript

When this command is executed, a src directory will be created under the project directory, and a module directory will be created inside the src directory. Using the -v option together with this command will show you that the project, src, and module directories were each created sequentially.

Figure 5. Linux Command mkdir: Creating multi-level directories
Figure 5. Linux Command mkdir: Creating multi-level directories

Precautions

Be Careful with Directory Names

When specifying directory names, make sure not to include special characters or spaces. Including special characters or spaces can lead to unintended results. If you need to use a directory name with spaces, enclose the name in double quotes (” “).

mkdir "directory name"
ShellScript

As shown in the figure below, if you do not enclose the name with spaces in quotes, it will create separate directories for each word (directory and name). Therefore, if the directory name contains spaces, always enclose it in quotes to achieve the desired result.

Figure 6. Linux Command mkdir: Handling spaces with quotes
Figure 6. Linux Command mkdir: Handling spaces with quotes

Conflicts with Existing Directories

If you try to create a directory with a name that already exists, an error message “File exists” will be displayed. Unless you intend to overwrite or delete the existing directory, use a different name to create the new directory.

Figure 7. Linux Command mkdir: Error message when trying to create a duplicate directory
Figure 7. Linux Command mkdir: Error message when trying to create a duplicate directory

Summary

The mkdir command is a very important command used to create directories in Linux. By learning the basic usage, various options, and useful applications, you can manage directories more efficiently. Using the -p option to create parent directories at once or the -v option to display detailed information can greatly enhance your workflow. Additionally, the ability to create multiple directories at once or complex directory structures in one command is extremely useful.

When using the mkdir command, be careful not to include special characters or spaces in directory names and avoid conflicts with existing directories. By following these precautions and effectively using the mkdir command, your tasks in the Linux environment will become much more convenient.

Now that you understand the basic concepts and usage of the mkdir command, try using it in various situations. It will greatly help in efficiently managing directory structures and increasing your productivity.

References

Leave a Comment