• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

mkdir if not exists

Thumbnail for Amanda KhooAmanda Khoo

Amanda Khoo

PhD, MSc in Data Science

Published: 2/1/2024

About Terminus

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

 $ mkdir -p 
Run in Warp

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

 $ mkdir -p foo/bar/test
Run in Warp

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:

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

Run in Warp

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:

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

Run in Warp

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

Without -p, mkdir has this behavior:

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

Run in Warp

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:

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

Run in Warp

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:

 $ mkdir -p  && cp 

Run in Warp

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

Written by

Thumbnail for Amanda KhooAmanda Khoo

Amanda Khoo

PhD, MSc in Data Science

Filed Under

Related Articles

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.

UnixLinux
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

Linux Chmod Command

Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.

Linux
Thumbnail for Razvan LudosanuRazvan Ludosanu

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

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.

Linux

Create Groups In Linux

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

Linux

Switch Users In Linux

Learn how to switch between users, log in as another user, and execute commands as another user in Linux.

Linux

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.

Linux

Delete Files In Linux

Learn how to selectively delete files in Linux based on patterns and properties using the rm command.

Linux

Find Files In Linux

Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.

Linux

Copy Files In Linux

Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.

Linux

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null