PQ
PQ.Hosting

Currency

The mkdir Command in Linux: Creating Directories, Flags and Permissions

Author
PQ
February 25, 2026
5 min read
18 views

mkdir stands for make directory. The command does exactly one thing — creates directories. But a few flags and some nuance around permissions make it considerably more useful than it looks at first glance.

Syntax

mkdir [options] directory_name

Create a single directory in the current location:

mkdir logs

Create using an absolute path:

mkdir /var/www/myapp

The -p Flag: Creating Nested Directories

The most important flag. Without it, mkdir will not create a directory if its parent does not exist — it throws an error instead.

mkdir projects/backend/api

If projects or backend do not exist — the command fails with No such file or directory.

With -p, the entire chain is created at once:

mkdir -p projects/backend/api

If intermediate directories already exist — no error occurs, the command just continues. This makes -p safe to use in scripts without checking whether the path exists beforehand.

Creating multiple independent paths in one command:

mkdir -p /opt/myapp/{logs,config,data,tmp}

The curly braces are expanded by bash, not by mkdir — this is called brace expansion. Result: four directories inside /opt/myapp/ with a single command.

The -m Flag: Permissions at Creation Time

By default mkdir creates a directory with 755 permissions (rwxr-xr-x) — the owner can do everything, others can only read and traverse. This is determined by the system's umask value.

The -m flag sets permissions at creation time, skipping a separate chmod call:

mkdir -m 700 private

The private directory will be accessible only to its owner — no one else can list its contents or enter it.

mkdir -m 755 public_html

Standard permissions for web directories: owner writes, others only read.

mkdir -m 1777 shared

The sticky bit (1 at the start) — classic permissions for shared directories like /tmp. Anyone can create files, but cannot delete files owned by others.

Note: the -m flag does not apply to intermediate directories created alongside -p — those get standard umask-based permissions. Only the final directory in the path receives the specified mode.

Creating Multiple Directories

Pass multiple names separated by spaces — mkdir creates all of them:

mkdir css js images fonts

With absolute paths:

mkdir /var/log/nginx /var/log/php /var/log/mysql

With brace expansion for a nested structure:

mkdir -p project/{src,dist,tests,docs}

Result: four directories inside project/. Can go deeper:

mkdir -p project/{src/{components,utils,hooks},dist,tests/{unit,integration}}

The -v Flag: Verbose Output

By default mkdir works silently. The -v flag prints a message for each created directory:

mkdir -pv /opt/app/{logs,config,data}

Output:

mkdir: created directory '/opt/app'
mkdir: created directory '/opt/app/logs'
mkdir: created directory '/opt/app/config'
mkdir: created directory '/opt/app/data'

Useful in scripts when visibility into what was actually created matters.

Permissions and Ownership

mkdir creates the directory under the current user — that user becomes the owner. Creating directories in system paths requires root:

sudo mkdir /etc/myapp

After creation, the directory often needs to be handed off to another user or service:

sudo mkdir /var/www/myapp
sudo chown www-data:www-data /var/www/myapp

Or create and set ownership in one chain:

sudo mkdir -m 750 /etc/myapp && sudo chown root:myapp /etc/myapp

Common Errors

mkdir: cannot create directory 'path': No such file or directory Intermediate directories do not exist. Fix: add the -p flag.

mkdir: cannot create directory 'path': File exists The directory already exists. With -p this error will not occur — existing paths are silently skipped.

mkdir: cannot create directory 'path': Permission denied No write permission in the parent directory. Either use sudo or verify the target path is correct.

Useful Combinations

Create a directory and immediately enter it

mkdir -p /opt/myapp && cd /opt/myapp

&& runs cd only if mkdir succeeded.

Create a temporary directory with a unique name

For temporary data, mktemp is better — it guarantees a unique name:

tmpdir=$(mktemp -d)
echo "Temp directory: $tmpdir"

Create a full project structure in one command

mkdir -p myproject/{src/{models,controllers,views},tests,config,logs,docs}

Check if a directory exists before creating it

Standard pattern in scripts:

[ -d /opt/myapp ] || mkdir -p /opt/myapp

Or just use -p directly, which handles existing paths on its own.

Quick Reference

Command What it does
mkdir dir Create directory in current location
mkdir /path/to/dir Create by absolute path
mkdir -p a/b/c Create full nested chain
mkdir dir1 dir2 dir3 Create multiple directories
mkdir -p project/{a,b,c} Create multiple nested at once
mkdir -m 700 dir Create with specific permissions
mkdir -pv path Create with verbose output
sudo mkdir /etc/dir Create a system directory

Summary

For most tasks mkdir -p covers everything — creates the full path at once and does not fail if parts of it already exist. The -m flag saves a separate chmod call when permissions matter from the moment of creation.

All of this works the same on any Linux server. To find a VPS, the PQ.Hosting catalog has options across a wide range of configurations and locations.

Share this article