How to Use Mkdir Only If a Directory Doesn’t Exist in Linux

Avatar

By squashlabs, Last Updated: October 16, 2023

How to Use Mkdir Only If a Directory Doesn’t Exist in Linux

To create a directory in Linux using the mkdir command only if the directory doesn’t already exist, you can use the -p option along with a conditional statement. Here are two possible ways to achieve this:

Method 1: Using Conditional Statement

You can use a conditional statement to check if the directory exists before running the mkdir command. Here’s an example:

if [ ! -d "/path/to/directory" ]; then
    mkdir "/path/to/directory"
fi

In this example, the command if [ ! -d "/path/to/directory" ] checks if the directory /path/to/directory does not exist. If it doesn’t exist, the mkdir "/path/to/directory" command is executed to create the directory.

You can replace /path/to/directory with the actual path of the directory you want to create.

Related Article: How to Calculate the Sum of Inputs in Bash Scripts

Method 2: Using the -p Option

The mkdir command in Linux has a -p option that allows you to create parent directories as needed. When using this option, the command will create the directory only if it doesn’t already exist. Here’s an example:

mkdir -p "/path/to/directory"

In this example, the command mkdir -p "/path/to/directory" creates the directory /path/to/directory if it doesn’t already exist. If the directory already exists, the command will do nothing.

You can replace /path/to/directory with the actual path of the directory you want to create.

Best Practices and Suggestions

– When using the conditional statement approach, make sure to enclose the path in quotes if it contains spaces or special characters. For example, if [ ! -d "/path/with spaces" ].

– It’s a good practice to use absolute paths when creating directories to avoid any ambiguity.

– If you need to create multiple directories at once, you can provide multiple paths to the mkdir command, separated by spaces. For example, mkdir -p "/path/to/dir1" "/path/to/dir2".

– If you are writing a script that requires creating directories, consider adding error handling to gracefully handle any failures that may occur during the directory creation process.

– If you want to create directories with specific permissions, you can use the -m option followed by the desired permissions. For example, mkdir -p -m 755 "/path/to/directory".

– If you want to create directories with different ownership, you can use the -m option followed by the desired ownership. For example, mkdir -p -m 755 -o username "/path/to/directory".

Related Article: Locating Largest Memory in Bash Script on Linux

Terminate Bash Script Loop via Keyboard Interrupt in Linux

Learn how to end a bash script loop using a keyboard interrupt in a Linux environment. Discover the keyboard interrupt signal in Linux and find out how to stop a loop in... read more

Adding Color to Bash Scripts in Linux

Adding color to Bash scripts in Linux can greatly enhance the readability of your output. This article will guide you in understanding color codes for Bash scripting,... read more

How to Handle Quotes with Md5sum in Bash Scripts

When working with md5sum bash scripts in Linux, it is important to understand how to handle quotes. This article provides a thorough look at the best practices for... read more

Accessing Seconds Since Epoch in Bash Scripts

Detailed instructions on how to access seconds since epoch in bash scripts in a Linux environment. Learn how to convert epoch time to a readable date format, get the... read more

Preventing Terminal Print from Bash Scripts in Linux

Learn how to prevent bash scripts from printing to the terminal in Linux. Discover various methods to redirect, suppress, or disable printing in your scripts. From... read more

Displaying Memory Usage in Bash Scripts on Linux

Learn how to display memory usage within a bash script on a Linux environment. This article covers various commands such as top, vmstat, sar, and ps, along with... read more