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 unzip Command: List and Extract ZIP Archives

Linux unzip Command: List and Extract ZIP Archives

Learn how to list and Extract ZIP Archives with the Linux unzip command, including practical examples, key options, and important precautions.

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 ip Command: Inspect and Configure Network Interfaces and Routes

Linux ip Command: Inspect and Configure Network Interfaces and Routes

Learn how to inspect and Configure Network Interfaces and Routes with the Linux ip command, including practical examples, key options, and important precautions.

Linux cat Command: Display and Concatenate File Contents

Linux cat Command: Display and Concatenate File Contents

Learn how to use the Linux cat command to display text files, concatenate multiple files, number lines, reveal hidden characters, and avoid unsafe redirection mistakes.

Linux awk Command: Analyze Text by Fields

Linux awk Command: Analyze Text by Fields

Learn how to analyze Text by Fields with the Linux awk 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.

Best Linux Distributions for Web Servers: Ubuntu, Debian, or Rocky Linux?

Best Linux Distributions for Web Servers: Ubuntu, Debian, or Rocky Linux?

Compare Ubuntu Server, Debian, and Rocky Linux for web hosting based on support lifecycle, package freshness, documentation, operational stability, and application requirements.

Linux help Command: Read Help for Bash Built-in Commands

Linux help Command: Read Help for Bash Built-in Commands

Learn how to use the Linux help command to read usage information for Bash built-in commands, with essential options, practical examples, output interpretation, and common troubleshooting tips.

How to Check the Linux Distribution, Version, Kernel, and Architecture

How to Check the Linux Distribution, Version, Kernel, and Architecture

Learn how to identify a Linux distribution and version with /etc/os-release, then check the running kernel and CPU architecture with hostnamectl and uname. The guide also covers scripts, legacy systems, containers, WSL, and chroot environments.