Skip to main content
Quick Tip

Make Directory Only if it Doesn't Exist Already in Linux

It's better to be careful and prevent errors while creating directories in case there are files or directories with the same name in the same location.

Team LHB

Warp Terminal

To create a directory in Linux, you use the mkdir command. It stands for 'make directory' after all. It's quite a simple command.

But, when you try to create a directory that already exists, you get an error like:

$ mkdir dir0
mkdir: cannot create directory ‘dir0’: File exists

This indicates that if a directory already exists, the mkdir command will not try to create or modify the already existing directory in any way.

But it also shows an error which is not what you always want especially if you are writing a bash script.

To prevent this error, use the -p flag along with mkdir command.

mkdir -p dirname

This way, though the directory is not created, it won't show any error as well. Your scripts will run as usual.

💡
You'll get the same error if a file or link exists with the same name as the directory you are trying to create. Why? Because everything is a file in Linux. A directory is a special file that acts like an index of all the files that are inside it.

Let's see things in a bit more detail here and discuss various ways of preventing this error.

Method 1: Use '-p' option

If you look at the manpage of the mkdir command, it has the '-p' flag.

The use of '-p' flag in mkdir is "no error if existing, make parent directories as needed".

When you use the '-p' flag, the mkdir utility will check if a directory, file, link or pipe with the same name does not exist already.

If the does exist, it will not modify your existing directory or file, nor will it show an error message.

mkdir -p dirname

no output because there exists none

This is very handy when you are creating custom bash scripts and don't want its execution to stop because of this error.

The -p can also be used to create a nested directory structure. If you want to create dir1/dir2/dir3 like directory structure and some or none of the directories exist in the hierarchy.

Method 2: Check if directory already exists in bash

If you are writing bash scripts, you can use the if condition and check if the directory already exists. If it doesn't then you create the directory.

Here's a sample code:

if [ -d my_dir ]
then
    mkdir my_dir
fi

Method 3: Send it to the void

A majority of UNIX tools have two output streams, stdout and stderr. Normally, both streams, stdout and stderr get printed to the terminal. But you can redirect either the normal output stream or the error stream to another file.

So, when the mkdir command throws an error to your terminal, you can redirect it to the void.

To redirect stdout, use it along with the '1' numerical stream descriptor, for stderr, use the numerical stream descriptor '2'. You can also redirect stdin by using the '0' stream descriptor.

To actually redirect the output, use the appropriate stream descriptor along with the redirection operator '>'

$ mkdir dir0 2> /dev/null

This will send the output of stderr to the /dev/null device which discards anything that gets written to it.

This is totally safe to do. As I mentioned earlier, if the directory already exists, then it will not be modified. Only an error message will be shown. All you are doing here is suppressing that error message.

Personally, I would go with the first method i.e. using the mkdir -p command for ensuring that the directory is created only if there is no file or directory of the same name in the same location.

Team LHB