Linux: create a folder only if it doesn't exist

Linux create bash directory

Under Linux, the command used to create a new folder is called "mkdir". Creating a directory is simple enough, but how do you create a folder if it doesn't exist? That's the question we're here to answer!

You have probably already used the mkdir command if you use Linux command on the command line to create a folder, regardless of the distribution you're used to using. Incidentally, this command also works under Windows.

1. The "mkdir -p" solution

To create a folder, for example named "folder" (I could have done better in terms of imagination), we use this command under Linux :

mkdir folder

This command creates the "folder" directory in the current location. If I run this command again, an error is displayed, as the folder already exists:

mkdir: unable to create "folder" directory: File exists

To avoid this error, there's a solution: the -p option of the mkdir command. If the directory already exists, an error will not be returned (which can be useful in some scripts) and if the parent directory doesn't exist either, it will be created as well. This is important information.

So, if I run :

mkdir folder -p

Which can also be written like this (with the option before the folder name) :

mkdir -p folder

The command will not return an error even if the folder already exists. How is this possible? Simply because with this option, the mkdir command takes the trouble to check whether the folder already exists or not.

Here's a preview of the "mkdir" command help, where you can clearly see the presence of the "-p" option, and the description is clear on how to use this option.

Create a folder if it doesn't exist Linux

Now, if I run this command :

mkdir /home/tutobox/donnees/folder -p

The command will create the " data "because it doesn't exist, and it will also create the " file" . Without the -p "an error would be returned because the file " data "is missing.

2. Solution #2: test whether a folder exists in bash

To test whether or not a folder exists, you can use a Bash scriptwhich allows you to perform other actions. If the folder already exists, perhaps we'll perform a complementary action, whereas if it doesn't exist, we'll create it.

In the script below, the FILE variable is used to determine the full path to the folder to be checked. Then, an "if/else" condition is used to indicate the action to be taken if it exists, and the action to be taken if it doesn't exist. If it doesn't exist, it will be created with mkdir. In both cases, an informative message is written to the Terminal.

#!/bin/bash
DOSSIER=/home/tutobox/dossier

if [ -d "$DOSSIER" ]; then
    echo "The folder exists ($DOSSIER)"
else
    echo "The folder does not exist ($DOSSIER). It will be created".
    mkdir $DOSSIER
fi

Create the script with the "nano" text editor or another editor such as "vi", depending on your usual preferences:

nano dossier.sh

Enter the above code in the "dossier.sh" script file, adapting the path of the folder to be checked, and adding any other actions, then save.

Add execution rights to the "dossier.sh" file, otherwise we won't be able to run it on the local machine:

chmod +x dossier.sh

Then run this script to test it:

./folder.sh

Here's an example of a return message in the Linux console. Of course, if the directory doesn't exist, the message will be different and the directory will be created by the script.

After reading this Tutorial Box article, you'll know how to use "mkdir -p" and how to test the existence of a directory with a Bash script. It's up to you to choose the method you prefer to use according to your needs.

Resources :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *