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.

Related Resources

More in This Category
Linux tmux Command: Detach and Reattach Terminal Sessions

Linux tmux Command: Detach and Reattach Terminal Sessions

Learn how to detach and Reattach Terminal Sessions with the Linux tmux command, including practical examples, key options, and important precautions.

Linux getent Command: Query Users, Groups, and Other NSS Databases

Linux getent Command: Query Users, Groups, and Other NSS Databases

Learn how to query Users, Groups, and Other NSS Databases with the Linux getent command, including practical examples, key options, and important precautions.

Linux fg Command: Bring a Shell Job to the Foreground

Linux fg Command: Bring a Shell Job to the Foreground

Learn how to bring a Shell Job to the Foreground with the Linux fg command, including practical examples, key options, and important precautions.

Linux free Command: Check Memory and Swap Usage

Linux free Command: Check Memory and Swap Usage

Learn how to use the Linux free command to inspect available memory, used memory, cache, and swap usage, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux apt Command: Manage Packages on Debian and Ubuntu

Linux apt Command: Manage Packages on Debian and Ubuntu

Learn how to manage Packages on Debian and Ubuntu with the Linux apt command, including practical examples, key options, and important precautions.

Linux readlink Command: Inspect Symbolic Link Targets

Linux readlink Command: Inspect Symbolic Link Targets

Learn how to use the Linux readlink command to display symbolic link targets and optionally resolve canonical paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux ethtool Command: Inspect Network Link and Driver Settings

Linux ethtool Command: Inspect Network Link and Driver Settings

Learn how to inspect Network Link and Driver Settings with the Linux ethtool command, including practical examples, key options, and important precautions.

Linux uniq Command: Remove or Count Adjacent Duplicate Lines

Linux uniq Command: Remove or Count Adjacent Duplicate Lines

Learn how to remove or count adjacent duplicate lines with the Linux uniq command, including practical examples, key options, and important precautions.

Linux gzip Command: Compress a File in gzip Format

Linux gzip Command: Compress a File in gzip Format

Learn how to compress a File in gzip Format with the Linux gzip command, including practical examples, key options, and important precautions.

Linux zip Command: Create ZIP Archives

Linux zip Command: Create ZIP Archives

Learn how to create ZIP Archives with the Linux zip command, including practical examples, key options, and important precautions.