Linux is a popular operating system used by millions of users around the world. One common task when working with Linux is creating and managing directories. In this article, we will cover how to create a directory in Linux only if it does not already exist. This is useful when writing scripts or when performing tasks that require creating multiple directories with unique names.

Using the ‘mkdir’ Command with ‘-p’ Option

The ‘mkdir’ command is the most basic way to create a directory in Linux. The ‘-p’ option allows you to create a directory only if it does not already exist, and it creates any necessary parent directories as well.

Syntax:

mkdir p /path/to/directory

Example:

mkdir -p /home/user/documents/important 

In this example, the ‘mkdir’ command will create the ‘important’ directory inside ‘/home/user/documents’ only if it does not already exist. If any of the parent directories do not exist, they will be created too.

Using a Shell Script with ‘if’ Statement and ‘[ -d ]’ Test

If you want to create a directory only if it does not exist in a shell script, you can use the ‘if’ statement and the '[ -d ]' test. The '[ -d ]' test checks if a directory exists.

Here is a sample shell script:

#!/bin/bash

directory=“https://tecadmin.net/home/user/documents/important”

if [ ! d “$directory” ]; then

    mkdir p “$directory”

    echo “Directory ‘$directory’ created.”

else

    echo “Directory ‘$directory’ already exists.”

fi

This script checks if the specified directory exists. If it does not exist, the script creates the directory using the ‘mkdir -p’ command and prints a message indicating that the directory was created. If the directory already exists, the script prints a message stating that the directory exists.

Using Python to Create a Directory If It Does Not Exist

If you prefer using Python for your scripting needs, you can use the ‘os’ module to check if a directory exists and create it if it does not.

Here’s a Python script that demonstrates this functionality:

import os

directory = “https://tecadmin.net/home/user/documents/important”

if not os.path.exists(directory):

    os.makedirs(directory)

    print(f“Directory ‘{directory}’ created.”)

else:

    print(f“Directory ‘{directory}’ already exists.”)

This script imports the ‘os’ module and defines the desired directory. It then checks if the directory exists using ‘os.path.exists()’. If the directory does not exist, it creates the directory using ‘os.makedirs()’ and prints a message indicating that the directory was created. If the directory already exists, the script prints a message stating that the directory exists.

Conclusion

Creating a directory only if it does not already exist is a common requirement in Linux, especially when working with scripts or automating tasks. This article discussed three methods to achieve this: using the ‘mkdir’ command with the ‘-p’ option, using a shell script with an ‘if’ statement and ‘[ -d ]’ test, and using a Python script with the ‘os’ module. Choose the method that best fits your needs and environment, and you’ll be able to create directories efficiently and effectively in Linux.