Last Updated on 2021-10-18 by Clay
mkdir
must be one of the most commonly used commands on Linux system, and its function is to create a folder (directory).
However, I have seen before that Linux users who are not familiar enough with this command have painstakingly created layer after layer of folders.
What does that mean? In fact, the directory he/she needs should be:
dir_1/dir_2/dir_3/dir_4
So, he/she use the following command to do it:
mkdir dir_1
mkdir dir_1/dir_2
mkdir dir_1/dir_2/dir_3
mkdir dir_1/dir_2/dir_3/dir_4
Use -p (–parents) parameter to make a recursive folder
If you check the manual page of mkdir
:
man mkdir
You will read the official instruction manual, which records:
-p, --parents
no error if existing, make parent directories as needed
That is to say, we enter the path of the production folder, if there is no parent folder, it will be created automatically.
In other words, we only need to use:
mkdir -p dir_1/dir_2/dir_3/dir_4
Output:
References
- https://unix.stackexchange.com/questions/49263/recursive-mkdir
- https://stackoverflow.com/questions/2336242/recursive-mkdir-system-call-on-unix
Hi
Good and very useful information about mkdir command. Every option is explained well and easy to understand.