Run Mkdir Only When the Directory Does Not Exist

Abdullah Bukhari Nov 03, 2022
  1. the mkdir Command and Its Purpose
  2. Create Directories With mkdir
  3. Create Directories With mkdir Only When They Don’t Exist
  4. Use mkdir With an if Control Structure
Run Mkdir Only When the Directory Does Not Exist

This article talks about mkdir and its usage in creating directories. It further discusses ways to use mkdir when the directory we want to make does not exist.

the mkdir Command and Its Purpose

The mkdir command in Unix/bash stands for make directory. It allows users to make folders, set permissions, and much more.

The user executing the mkdir command must have sufficient privileges to create a directory in the path specified, or the user may get a permission denied error.

Create Directories With mkdir

The general syntax for mkdir is as follows:

mkdir <specified_option> directory_name
sudo mkdir <specified_option> directory_name # in case user doesn’t have permissions

mkdir also allows the creation of multiple directories at once. To do so, add comma-separated directory names within curly brackets.

Ensure that you do not include extra spaces in the directory names, or the directories will include the spaces in their names. The command below demonstrates what we said:

mkdir {dir1,dir2,dir3} # here directory names are dir1, dir2 and dir3

Creating Directories With mkdir

Create Directories With mkdir Only When They Don’t Exist

One issue when using mkdir is an error when the directory one is trying to create already exists. It is not much of a problem running the bash command as mkdir makes a directory if it does not exist; otherwise, it just gives an appropriate error.

However, there’s a twist. Suppose you’re trying to code a custom bash shell and don’t want mkdir to give an error when trying to recreate an existing file. Then what do you do?

We will explain several ways to prevent the above.

[[ -d dir_name ]] || mkdir dir_name # option 1
mkdir -p dir_name # option 2
mkdir dir_name 2>/dev/null # option 3

Creating Directories With mkdir Only When They Dont Exist

The first option and third options are more recommended than the second one, as the second one might run into race conditions during concurrent execution. As for the third one, it redirects the output of stderror to a file dev/null.

If you have studied operating systems, there is a concept of the ppfdt table (process file descriptor table). Recall that by default, the zero descriptor points to stdin, one descriptor points to stdout, and two descriptor points to stderror.

/dev/null is a null device file, which will discard anything and everything written to it and return an end-of-file character on being read. It is like a vacuum, which will suck anything thrown into it.

That being said, here you can redirect stderror to any unused file of your choice, but /dev/null is more recommended.

Use mkdir With an if Control Structure

Another way to do it could revolve around using an if control structure. You could check if the directory exists and if it doesn’t, then proceed to create it; otherwise, do nothing.

The below script elaborates on it:

if [ ! -d dir_name ]; then mkdir dir_name; fi

Using mkdir With an if Control Structure

Related Article - Bash Directory