Bash – Create new directory
To create a new directory in Bash scripting, you can use mkdir
command. Call the mkdir
command and pass the directory name as argument.
The syntax to create a new directory using mkdir
command is given below.
mkdir directory_name
To create more than one directory in a single command, pass the directory names as arguments to mkdir command, as shown in the following.
mkdir directory_name_1 directory_name_2 directory_name_3
If given directory path has any parent directories, we can use -
p option with mkdir
command to create non existent directories, as shown in the following.
mkdir -p parent/inner_parent/directory
Example
In the following script, we create a new directory named resources
, using mkdir
command.
example.sh
#!/bin/bash
mkdir resources
echo "Directory created successfully."
Bash Version: GNU bash, version 5.2.15(1)-release (aarch64-apple-darwin22.1.0)
Output
sh-3.2# bash example.sh
Directory created successfully.
Conclusion
In this Bash Tutorial, we learned how to create a new directory using mkdir
command.