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

Avatar

By squashlabs, Last Updated: Oct. 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 Handle Quotes with Md5sum 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

Related Article: How to Loop Through an Array of Strings in Bash

- 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".

Locating and Moving Files in Bash Scripting on Linux

Learn how to locate and move files in Linux using bash scripting. This article covers file search, manipulation, handling, and navigation techniques,… read more

How to Run Bash Scripts in the Zsh Shell

Bash scripts are a common tool used in the Linux environment for automating tasks. However, the Zsh shell has gained popularity as an alternative to … read more

How to Filter Strings in Bash Scripts

Pattern-based string filtering is a powerful technique that can greatly enhance your Linux bash scripting skills. In this article, you will learn how… read more

Creating Incremental Backups with Bash Scripts in Linux

Implementing incremental backups in Linux using bash scripts is a valuable skill for any system administrator or developer. This in-depth tutorial co… read more

Comparing Array Sizes in Bash Scripting on Linux

Learn how to compare array sizes in bash scripting on Linux with a variety of code snippets and commands. Discover different approaches such as using… read more

How to Use Getopts in Bash: A Practical Example

Getopts is a powerful tool in Bash that allows you to handle command-line options and arguments in your scripts. This article will guide you through … read more

How to Hide & Validate User Input in Bash Scripts

Securing user input in bash scripts is a critical aspect of Linux system administration. This article covers techniques to hide and validate user inp… read more

Executing Bash Scripts at Startup in Ubuntu Linux

Learn how to configure your Ubuntu Linux system to automatically run a bash script at startup. This article covers various methods, including using s… read more

How to Use the Ls Command in Linux

Learn how to use the Ls command in Linux with this tutorial. From understanding the syntax and parameters to exploring real-world examples and advanc… read more

How to Use SFTP for Secure File Transfer in Linux

Securely transferring files between a local machine and a remote server is essential in Linux environments. This article provides a step-by-step tuto… read more