How to Make Directory Only if it Doesn’t Exist in Linux

1. Use the mkdir command with the -p flag:

mkdir -p /path/to/directory

This will create the directory only if it doesn’t already exist.

2. Use the test command with the -d flag:

test -d /path/to/directory || mkdir /path/to/directory

This will check if the directory exists and only create it if it doesn’t.

As a Linux user, you might find yourself needing to create a directory frequently. While it may be a simple task, there may be occasions where you want to create a directory only if it does not exist. Luckily, this can be done quickly and easily in Linux.

In this guide, you will explore the steps needed to create a directory if it does not exist in Linux. And you will also know about some of the advantages of doing so and when you might want to use this approach.

How to Create/Mkdir a Directory If It Does Not Exist in Linux

By using the following methods, you can create a directory only if it does not exist in linux:

  • Method 1: Using the “mkdir” command

  • Method 2: Using the “test” command

Method 1: Using the “mkdir” command

The “mkdir” command is a standard Linux command that is used to create directories. By default, the “mkdir” command creates a directory only if it does not exist. However, if you try to create a directory that already exists, you’ll receive an error message.

To create a directory if it does not exist, you can use the “-p” option with the “mkdir” command. This option tells the command to create the directory and any parent directories that do not exist. Here’s an example:

mkdir -p /path/to/directory

In this example, the “mkdir” command will create the directory “/path/to/directory” only if it does not exist. If the directory already exists, the command will not do anything.

Method 2: Using the “test” command

Another way to check if a directory exists before creating it is to use the “test” command. The “test” command checks if a file or directory exists and returns a success or failure status code.

To check if a directory exists, you can use the “-d” option with the “test” command. This option checks if the given directory exists and returns a success status code if it does. Here’s an example:

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

In this example, the “test” command is used to check if the directory “/path/to/directory” exists. If the directory does not exist, the “mkdir” command is executed to create the directory.

Here are some basic faq’s on how to create a directory if it does not already exists in linux:

Q: Can I create a directory with different permissions if it does not already exist?

A: Yes, you can use the chmod command to set the permissions for the directory after creating it. For example: mkdir -m 755 /path/to/directory will create the directory with permissions set to rwxr-xr-x.

Q: Is it possible to create multiple directories at once using the mkdir command?

A: Yes, you can provide multiple directory names separated by spaces to the mkdir command. For example: mkdir /path/to/dir1 /path/to/dir2 will create two directories named dir1 and dir2 under the /path/to directory.

Q: Is it possible to create a directory with a specific owner or group using the mkdir command?

A: Yes, you can use the chown and chgrp commands to set the owner and group for the directory after creating it. For example: mkdir /path/to/directory && chown user:group /path/to/directory will create the directory and set the owner to “user” and the group to “group”.

Q: Can I use the mkdir command to create directories with spaces in their names?

A: Yes, you can use the mkdir command to create directories with spaces in their names by enclosing the directory name in quotes. For example: mkdir "/path/to/directory with spaces" will create a directory named “directory with spaces” under the /path/to directory.

Advantages of creating a directory only if it does not exist

Creating a directory only if it does not exist has several advantages. For one, it helps prevent accidentally overwriting existing directories or files. Additionally, it can help make your scripts more robust and easier to maintain. If a script tries to create a directory that already exists, it could cause unexpected behavior or errors. By checking if the directory exists first, you can ensure that your scripts behave as expected.

Conclusion

In conclusion, creating a directory if it does not exist in Linux is a simple task that can be done in a couple of different ways. By using the “mkdir” command with the “-p” option or the “test” command, you can create a directory only if it does not exist. This approach can help prevent unexpected errors and make your scripts more robust.

Recommended Tutorials

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?