Home / Blog / mkdir Command in Linux: A Complete Guide to Creating Directories

mkdir Command in Linux: A Complete Guide to Creating Directories

Learn how to use the Linux mkdir command to create directories and nested folder structures, create multiple directories, set permissions, use parent directories, and efficiently organize files from the command line.

mkdir Command in Linux: A Complete Guide to Creating Directories

mkdir command Linux, Linux mkdir command, create directory Linux, make directory Linux, Linux folder commands, mkdir command examples, Linux commands for beginners

mkdir Command in Linux: A Complete Guide to Creating Directories

Linux provides many command-line utilities that help developers and system administrators manage files and directories. One of the most commonly used commands is mkdir, which is used to create new directories from the terminal.

Whether you are creating a project folder, organizing application files, preparing a server directory, or building a nested folder structure, the mkdir command provides a simple and efficient way to create directories in Linux.

Understanding the mkdir command is therefore essential for anyone working with Linux, shell scripting, development environments, or server administration.

What Is the mkdir Command in Linux?

The mkdir command stands for make directory. It is used to create one or more new directories.

Basic syntax:

mkdir DIRECTORY_NAME

For example:

mkdir project

This creates a new directory named project in the current working directory.

You can verify the directory using:

ls
Create a Single Directory

The simplest way to create a directory is:

mkdir documents

This creates a directory called documents.

You can then move into it using:

cd documents

This is one of the most basic and frequently used Linux directory-management operations.

Create Multiple Directories

The mkdir command can create multiple directories with a single command.

For example:

mkdir documents images videos

This creates three directories:

documents
images
videos

This is useful when you need to quickly create several folders for a project or application.

Create Nested Directories

You can create a directory inside another directory by specifying the complete path.

For example:

mkdir project/src

However, the project directory must already exist.

If it does not exist, Linux may return an error.

To create both directories at the same time, use the -p option.

Create Parent Directories With mkdir -p

The -p option allows you to create parent directories when they do not already exist.

For example:

mkdir -p project/src/components

This can create:

project/
└── src/
    └── components/

If the parent directories do not exist, mkdir -p creates them automatically.

This is particularly useful for creating project structures.

Create a Project Directory Structure

Suppose you want to create a basic application structure:

myapp/
├── src/
├── public/
├── config/
└── logs/

You can create it with:

mkdir -p myapp/src myapp/public myapp/config myapp/logs

This allows you to prepare multiple directories quickly without creating each parent directory manually.

Create Directories Using Absolute Paths

You can specify an absolute path when creating a directory.

For example:

mkdir /var/www/myapp

This attempts to create the myapp directory inside /var/www.

Creating directories in system locations may require appropriate permissions.

For example:

sudo mkdir /var/www/myapp

The sudo command runs the operation with elevated privileges when the current user does not have permission to write to the target location.

Create Directories Using Relative Paths

A relative path starts from your current working directory.

For example:

mkdir projects

creates the directory inside the current directory.

You can check your current location using:

pwd

For example:

pwd

may return:

/home/user

Running:

mkdir projects

will therefore create:

/home/user/projects
Create Directories With Spaces in Their Names

If a directory name contains spaces, enclose it in quotes.

For example:

mkdir "My Projects"

This creates a directory named:

My Projects

You can also escape the space:

mkdir My\ Projects

Using quotes is generally easier to read.

Create Directories With Specific Permissions

The -m option allows you to specify the directory permissions when creating the directory.

For example:

mkdir -m 755 public

This creates the public directory with the specified permission mode.

Another example is:

mkdir -m 700 private

This can be useful when creating directories that should have restricted access.

The effective permissions can also be affected by the system's umask.

Check Directory Permissions

After creating a directory, you can use:

ls -ld public

This displays information about the directory itself, including its permissions and ownership.

For example:

drwxr-xr-x 2 user user 4096 Sep 3 public

The permission information helps you understand who can read, write, or access the directory.

Create Directories Without Errors if They Already Exist

Normally, running:

mkdir project

when project already exists produces an error.

You can use:

mkdir -p project

The -p option allows the command to succeed when the specified directory already exists.

This behavior is especially useful in shell scripts and automation.

Create Directories in Shell Scripts

The mkdir command is frequently used in shell scripts.

For example:

#!/bin/bash

mkdir -p logs
mkdir -p backups
mkdir -p uploads

echo "Directories created successfully."

This can automatically prepare required directories when deploying or configuring an application.

Use mkdir for Application Deployment

Application deployments often require directories for different types of files.

For example:

mkdir -p /var/www/myapp
mkdir -p /var/www/myapp/logs
mkdir -p /var/www/myapp/uploads

This creates the required application directories.

Deployment scripts can use mkdir -p to ensure required folders exist before copying files or starting an application.

Use mkdir for Backup Directories

The command can also be useful when creating backup locations.

For example:

mkdir -p backups/2026/september

This creates the required parent directories automatically.

You can then store backup files inside the newly created directory.

Use mkdir With Variables

Shell variables can be used with mkdir.

For example:

DIRECTORY="myapp"

mkdir -p "$DIRECTORY"

The directory name is stored in the DIRECTORY variable.

Quoting the variable is a good practice because it prevents unexpected behavior when directory names contain spaces or special characters.

Common mkdir Options

Some commonly used mkdir options include:

-p    Create parent directories as needed
-m    Set directory permissions
-v    Display a message for each directory created

For example:

mkdir -pv project/src/components

The -v option provides information about directories being created.

Create Directories Verbosely

The -v option enables verbose output.

For example:

mkdir -v projects

You may see output indicating that the directory was created.

This can be helpful when running commands manually or troubleshooting shell scripts.

Common Mistakes With mkdir
Creating a Directory Without Its Parent

Running:

mkdir project/src

will fail if project does not already exist.

Use:

mkdir -p project/src

when you want Linux to create the parent directory automatically.

Forgetting Permissions

Trying to create a directory in a protected location can result in a permission error.

For example:

mkdir /var/www/myapp

may fail if your user does not have the required permissions.

In situations where elevated privileges are appropriate, you may need:

sudo mkdir /var/www/myapp
Using Unquoted Directory Names

A directory name containing spaces should be quoted.

Instead of:

mkdir My Projects

use:

mkdir "My Projects"

Otherwise, the shell interprets the two words as separate arguments.

Accidentally Creating Unwanted Directories

Be careful when using variables or dynamically generated paths.

For example:

mkdir -p "$DIRECTORY"

is safer than constructing paths without proper quoting.

Best Practices
Use mkdir to create directories from the Linux command line.
Use mkdir -p when creating nested directory structures.
Use quotes around paths that contain spaces.
Use mkdir -m when a specific permission mode is required.
Use mkdir -v when you need confirmation about created directories.
Verify newly created directories with ls.
Use pwd to confirm your current working directory before creating relative paths.
Use appropriate permissions when creating directories in shared or production environments.
Quote shell variables used as directory names.
Test directory-creation commands carefully in production scripts.
mkdir Command in Linux Administration

System administrators frequently use mkdir to create directories for:

Application files
Server configuration
Logs
Backups
User data
Temporary files
Deployment environments
Web applications

For example:

sudo mkdir -p /var/www/myapp/logs

can be used when preparing a directory structure for a web application.

mkdir Command for Developers

Developers commonly use mkdir when setting up project structures.

For example:

mkdir -p myproject/src myproject/tests myproject/config

This creates separate directories for source code, tests, and configuration.

The command is useful across many development environments, including Node.js, PHP, Python, Java, and other Linux-based development workflows.

mkdir Command in DevOps

The mkdir command is also widely used in DevOps workflows.

It can help create directories for:

Build artifacts
Deployment files
Application logs
Backups
Temporary data
CI/CD workflows
Docker-related files
Server deployments

For example:

mkdir -p deployment/releases deployment/logs deployment/backups

A deployment script can then place the required files into the appropriate directories.

mkdir Command in Docker and Containers

The mkdir command can also be used inside Dockerfiles and container environments.

For example:

RUN mkdir -p /app/logs

This creates the required directory inside the container image.

Applications can then use the directory for logs or other runtime files.

mkdir Command in Cloud Environments

Linux-based cloud servers frequently require directory structures for applications, logs, backups, and deployment files.

For example:

mkdir -p /opt/myapp/config
mkdir -p /opt/myapp/logs
mkdir -p /opt/myapp/data

This can help organize application resources on cloud-hosted Linux servers.

Conclusion

The Linux mkdir command is a simple but essential utility for creating directories and organizing files. From creating a single folder to building complete nested directory structures, mkdir provides several options for managing filesystem organization.

The -p, -m, and -v options make the command particularly useful for development, scripting, server administration, and DevOps workflows.

For Linux beginners, developers, and system administrators, understanding how to use mkdir effectively is an important step toward becoming comfortable with the Linux command line.

Contact Us

1119 W Duarte Rd, Arcadia, CA 91007

Solace Infotech Pvt. Ltd, Supreme HQ,
          HQ3C+9F2, Yash Orchid Society,
          Baner, Pune, Maharashtra 411021

4th Floor, Samraat Nucleus,
           Mumbai Naka, Nashik - 422001