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.
Table of Contents
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
ShellScriptFor example, if you want to create a directory named test
, you would enter the following:
mkdir test
ShellScriptWhen this command is executed, a directory named test
is created in the current working 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
ShellScriptFor 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
ShellScriptWhen this command is executed, the parent
directory will be created if it does not exist, and the child
directory will be created inside it.
-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
ShellScriptFor example, to create a directory named verbose
and display detailed information, you would enter:
mkdir -v verbose
ShellScriptWhen this command is executed, a message like “mkdir: created directory ‘verbose'” will be displayed, confirming that the verbose
directory was created.
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
ShellScriptFor example, to create three directories named dir1
, dir2
, and dir3
at once, you would enter:
mkdir dir1 dir2 dir3
ShellScriptAs shown in the following figure, using the -v
option allows you to clearly see which directories were created.
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
ShellScriptWhen 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.
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"
ShellScriptAs 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.
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.
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.