Mkdir Create Multiple Directories in One Command
The mkdir
command is one of the most useful commands for quickly and efficiently creating directories in Unix-like systems in the terminal.
When getting started with the mkidr
command, you might be wondering, how can I create multiple or nested directories in one command?
This is what we are here for. In this tutorial, we are going to show you how you can use the mkdir command to create nested directories in a single command.
Quick Fix
In Unix-like systems, the way to create multiple and nested directories using the mkdir
command is by using the -p
flag as shown in the syntax below:
mkdir -p <path>
Where the path
refers to the path containing the list of nested directories you wish to create, separated by a forward slash character.
For example:
mkdir -p /src/repos/logs/audit/incremental
The command above will first create the directory called src
in the root directory, It will then create the directory, repos
inside the src
and so on and so on. Each directory will be created inside the directory above it.
NOTE: If an immediate directory already exists, the command will ignore it and proceed with no errors.
Verbose
By default, the mkdir
command will silently performing the creation and skip (existing directories) without any output. However, you can enable verbose mode to show the operation perfomed by the comand using the -v
parameter:
mkdir -v -p <path>
Multiple Directories in Parent Directory
in some cases, you might want to create multiple directories at once within the same parent directory. For that we can use the command syntax as shown:
mkdir -p <path> ...
For example:
mkdir -p src/auth/logs /var/
This shoudl create the src
, auth
, and logs
within the /var
directory.
Conclusion
In this tutorial, we learned how to quickly and efficiently create multiple directories or nested directories using a single command in the mkdir command.