Terminal create directory if it doesn't exist

January 24, 2021

If you want to create a directory using bash (in the terminal), but you don't know whether it'll already exist so you only want to create it if the directory doesn't exist yet:

mkdir -p directory
mkdir -p my/directory/here

You can use this with a top-level directory (the first example), or with a deeply nested directory (the second example).

The -p flag creates all directories necessary along the way (for example my, directory, and here) if they don't exist yet.

A quick favor: was anything I wrote incorrect or misspelled, or do you still have questions? Please use this form to let me know or ask for help!

Loading...