Linux mkdir Command: Create Directories and Parent Paths
This guide explains how to create directories with Linux mkdir, build missing parent paths, set permissions, understand umask behavior, and verify results in scripts. It focuses on practical Linux usage, predictable automation, and the cases where a seemingly small path or option mistake can change the result.
The examples use GNU Coreutils behavior. Check mkdir --help, man mkdir, and your distribution documentation when portability or a version-specific option matters.
What Is the mkdir Command?
mkdir creates directories with the specified names. By default, it fails if a parent path is missing or if the requested name already exists. The -p option creates missing parents and accepts existing directories.
Basic Syntax
mkdir [OPTION]... DIRECTORY...
Quote paths that contain spaces. When supported, place -- before operands that begin with a hyphen so they are not interpreted as options.
Basic mkdir Usage
Create one directory
Use a relative or absolute path. The parent directory must already exist unless -p is used.
mkdir logs mkdir /srv/myapp
Create several directories
Each path is a separate operand. Quote names that contain spaces.
mkdir src tests docs mkdir 'project files'
Create missing parent directories
The -p option builds the required hierarchy and makes repeated initialization commands easier to run safely.
mkdir -p project/src/components
mkdir -p /srv/myapp/{bin,config,logs}
Checking the Created Path
mkdir usually produces no output if it completes successfully. -p creates any missing parent directories as well.
mkdir -p demo-dir/logs find demo-dir -type d
Example output:
demo-dir demo-dir/logs
Even if the directory is created, it does not mean the permissions are as expected. Check the actual permissions with stat -c '%a %n' demo-dir demo-dir/logs.
Key Options
| Option | Purpose |
|---|---|
-p, --parents |
Create missing parents and ignore existing directories. |
-m MODE |
Set the mode of explicitly named new directories. |
-v |
Print each directory that is created. |
-Z |
Apply the default SELinux context where supported. |
--context=CONTEXT |
Set a specified security context on supported systems. |
Long options often make reviewed scripts easier to understand. When a script must run on non-GNU systems, verify every non-POSIX option on the target platform.
Practical Examples
Create a directory with a mode
The mode uses the same numeric or symbolic notation as chmod. Confirm ownership and parent permissions as well.
mkdir -m 750 private-data mkdir -m u=rwx,go= project-secret
Control permissions on parent directories
With -p, -m does not guarantee the same mode for every newly created parent. Set an appropriate umask or adjust the hierarchy afterward.
( umask 027; mkdir -p app/cache/data )
find app -type d -exec chmod 750 {} +
Verify the result
Check exit status in automation and inspect the actual modes when the directory contains private or shared data.
mkdir -pv deploy/releases/current stat -c '%A %U:%G %n' deploy deploy/releases deploy/releases/current
mkdir and Related Commands
| Command or method | Primary role | Best fit |
|---|---|---|
mkdir |
Create ordinary directories | Workspaces and directory trees |
install -d |
Create directories and set attributes | Installation and deployment |
rmdir |
Remove empty directories | Safe cleanup of empty paths |
mktemp -d |
Create an unpredictable temporary directory | Temporary script workspaces |
Important Cautions
-p also accepts a mistyped path
A spelling error can become a new parent directory instead of an immediate failure. Validate important system paths and variables first.
Default permissions depend on umask
Without -m, the process umask restricts the initial mode. Inspect the actual result instead of assuming one fixed value.
Creation depends on parent permissions
Creating a child requires suitable write and search permissions on the parent path. Check the complete path when permission errors occur.
FAQ
How do I create all missing parent directories?
Use mkdir -p PARENT/CHILD/DIRECTORY.
How do I avoid an error when the directory already exists?
Use -p. It still fails if the path is occupied by a non-directory.
Does mkdir -m apply the same mode to parents created by -p?
Not necessarily. Manage parent modes with umask or a separate chmod step.
Should I use mkdir for secure temporary directories?
Use mktemp -d when a unique, unpredictable temporary name is required.









