Table of Contents
- Introduction
- How to create a new directory – mkdir linux
- How to create parent directories – mkdir linux
- Conclusion
Introduction
Are you wondering if it is possible to create a new directory using the linux command line directly? You are in the right place!
This tutorial will show you how to create a new directory using the mkdir
command.
Since this command has several options, we will also see the most common and useful ones.
How to create a new directory – mkdir linux
To create a new directory in linux you can use the mkdir
command.
Let’s see now the structure of mkdir command:
mkdir [OPTIONS] [DIRECTORY]
The simplest example we can do is to create a new directory in the same working directory.
Suppose we want to create a directory called my_new_dir
.
To do this, just open a linux command line and type the following command:
mkdir my_new_dir
Using the ls command, you can verify that the new directory was created successfully.
If you don’t know how to best use the ls command, I suggest you take a look at this post!
Another example we can do is to create a new directory but in a completely different path.
mkdir /tmp/my_new_dir
In this case the my_new_dir
has been created in the tmp
folder.
As you can see linux allows you to create a new directory anywhere you want, just specify the full path.
How to create parent directories – mkdir linux
It may happen that a directory is inside others, thus creating the classic tree structure.
In this case, if directories prior to the one you want to create do not exist, an option to the mkdir
command can be used.
Let’s see an example to understand better.
Suppose we want to create this directory structure:
- tmp
- my_first_dir
- my_second_dir
- my_new_dir
Starting from the tmp
directory which already exists, we don’t have to repeat the mkdir
command 3 times like this:
mkdir /tmp/my_first_dir
mkdir /tmp/my_first_dir/my_second_dir
mkdir /tmp/my_first_dir/my_second_dir/my_new_dir
Instead, just use the -p
option to create everything in one command:
mkdir -p /tmp/my_first_dir/my_second_dir/my_new_dir
The -p
option of the mkdir
command in fact allows us to create all directories that do not exist for that path.
Conclusion
If you have come this far, it means that you have now learned how to create a directory in Linux using the mkdir command.
As always, I recommend that you continue with the next article to learn everything there is to know about the Linux world.
Feel free to leave a comment and ask me for help if you are in trouble.