Table of Contents
To run the following commands, directly write the command in the bash terminal and execute them.
Using mkdir -p
Command
Use the mkdir -p
command to create folder if not exists in Bash
1 2 3 |
mkdir -p /path/to/directory |
The mkdir
is used to create the directory. The -p
created all the intermediate directories if they didn’t exist.
The /path/to/directory
is the path to the directory you wanted to create. The command created the directory that does not exist and also parent directories that did not exist for the given path.
For example:
1 2 3 |
mkdir -p test/bash/scripts |
It will create folders test, test/bash, and test/bash/scripts if they don’t exist.
Using test -d
Command with ||
Operator
Use the Test -d
command to test whether a directory/folder exists. The mkdir
command creates a directory/folder if it does not exist.
1 2 3 |
test -d /path/to/directory || mkdir /path/to/directory |
The test -d
command is used to check whether the directory exists. The OR operator (||
) executes the command on the right-hand side of itself if the left-hand side command is not True
. The mkdir
is used to create the directory. The /path/to/directory
is the path to the created directory.
The following commands cannot be run directly on the terminal but by calling the script file. First, make a bash file using a text editor and write the code in that file. Then save the file with the
.sh
extension.After saving the file, open the terminal and execute the
source FileName.sh
command to run the bash script file. Note that the following conditional commands can be used to check and create a folder if it does not exist in bash.
Using if
Statement
The if
conditional statement tests whether a directory/folder exists. If the condition meets and the directory/folder does not exist, it will create a directory/folder using the mkdir
command.
1 2 3 4 5 |
if [ !-d /path/to/directory]; then mkdir /path/to/directory fi |
The command checked the directory path /path/to/directory
does not exist, the test command returned True
, and the mkdir
command was executed to create the directory. If the directory already exists, the test command returns False
, and the script skipped themkdir
command and continues executing subsequent commands.
In the above example,
[]
is the test command.
Using mkdir
with if
Statement
Use if
conditional statement with mkdir
command to create folder if it not exists in Bash..
1 2 3 4 5 |
if ! mkdir /path/to/directory 2>/dev/null && [ ! -d /path/to/directory]; then echo "Failed to create directory" >&2 fi |
The mkdir
command returned True
if the folder /path/to/directory
does not exist. If the directory already exists, or if the mkdir
command failed, the next condition will be executed, testing whether the directory exists. If the directory does not exist, the script executes the echo
command and displays an error message Failed to create directory
to the console.
Using if
Statement with stat
Command
The if
conditional statement with stat
command to create folder if it not exists in Bash.
1 2 3 4 5 |
if [ ! -d /path/to/directory]; then stat /path/to/directory 2>/dev/null || mkdir /path/to/directory fi |
The if
statement was True
; if the directory /path/to/ directory
does not exist, the stat
command will be executed. If the stat
command failed (i.e., the directory does not exist), the mkdir
command was executed to create the directory. If the directory already exists, the stat
command succeeded, and the mkdir
command is not executed.
That’s all about bash create folder if not exists.