Bash Make Directory

mkdir creates new directories, and its -p flag lets you create a whole nested folder structure in a single command.

Creating a Single Directory

mkdir takes one or more names and creates a new, empty directory for each one in the current location. If a directory with that name already exists, mkdir refuses and prints an error rather than silently doing nothing.

Create one folder

$ mkdir photos
$ ls -l
drwxr-xr-x 2 user user 4096 Jul 16 10:05 photos

Creating Several Directories at Once

List multiple names to create several sibling directories in one call. This is handy for setting up a standard project layout in a single step.

Create a project skeleton

$ mkdir src tests docs
$ ls
docs  src  tests

Creating Nested Paths with -p

Without -p, mkdir fails if any parent directory in the path is missing. The -p (parents) flag creates every missing directory along the path automatically, and it also stays silent instead of erroring if the final directory already exists — which makes it safe to rerun in scripts.

Create a deep path in one command

$ mkdir -p project/src/components/auth
$ find project -type d
project
project/src
project/src/components
project/src/components/auth
  • mkdir name — create one directory
  • mkdir a b c — create several directories at once
  • mkdir -p a/b/c — create a nested path, making parents as needed
  • mkdir -p existing_dir — succeeds quietly even if the directory is already there
  • mkdir -v name — print a message confirming each directory created
FlagMeaning
-pCreate missing parent directories; no error if the target already exists
-vVerbose — print a line for every directory created
-m MODESet the permissions on the new directory (e.g. -m 755)
Note: Without -p, mkdir a/b/c fails with 'No such file or directory' the moment a or a/b doesn't already exist — always reach for -p when scaffolding nested folders.