Back

HOWTO!

mkdir without warning if directory already exists

This is a fairly simple trick on how to avoid the warning message you get when you use mkdir to create a new directory, but the directory you want to create already exists. Quite useful when doing bash scripts or automating your environment for instance.

mkdir with warning

This is the default behaviour of the mkdir command:

$ mkdir my_directory
$ mkdir my_directory
$ mkdir: my_directory: File exists

mkdir without the warning

I would recommend to do this to check if the directory actually exist before trying to mkdir.

$ [ -d my_directory ] || mkdir my_directory

Second solution - check if the directory actually exist

This is another way we can avoid the warning message:

$ mkdir my_directory
$ mkdir -p my_directory

Notice the -p parameter. If the directory you want to create already exists, this will not return any messages in the command line.