mkdir if not exists

Amanda Khoo
Amanda KhooPhD, MSc in Data Science
Published: February 1, 2024

To create new directories if they do not exist and ignore the command if they do (no error message) use:

Bash
$ mkdir -p

The -p flag also allows for subdirectories to be created, if they do not already exist. For example:

Bash
$ mkdir -p foo/bar/test

will create the folder “test” within the folder “bar” within the folder “foo”.

When should I use mkdir vs mkdir -p?

mkdir -p can handle creating subdirectories in one command. An additional feature of adding the -p flag is the lack of an error message when the directory already exists. For example, to create subdirectories using mkdir you would need to do:

Bash
$ mkdir foo #create the folder “foo”
 $ cd foo #navigate into the folder “foo”
 $ mkdir bar #create the folder “bar” inside the folder “foo”

adding the -p flag allows you to create the subdirectory bar inside the directory foo (even if foo doesn’t exist yet) in one line:

Bash
$ mkdir -p foo/bar #creates the folder foo and the folder bar inside foo

Using mkdir -p to ignore a directory if it already exists

Without -p, mkdir has this behavior:

Bash
$ mkdir foo #create foo
 $ mkdir foo #foo exists now, command errors out

will return the error, “File exists”. But, when used in conjunction with the -p flag, the command to create a new directory is instead ignored, with no error returned:

Bash
$ mkdir -p foo #create foo
 $ mkdir -p foo #foo exists now, command is ignored

So mkdir -p is useful if you want to make subdirectories quickly, and when using the mkdir command in situations where you want the command to be ignored (no error) if the directory already exists – for example, when using mkdir in combination with other commands to create efficient pipelines for data writing and storage.

Create directory if missing while using cp

mkdir -p can be useful in combination with other commands like cp (copy). Combining the mkdir and cp commands is a powerful way to create directories that don’t exist and copy them over all in one go:

Bash
$ mkdir -p  && cp

where the && operator combines the two commands, and the cp command stands for “copy”.

Written by
Amanda Khoo
Amanda KhooPhD, MSc in Data Science
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.