mkdir

file managementlinux
The mkdir command is one of the most frequently used commands in Linux/Unix-like operating systems. mkdir The mkdir command is used to create directories (folders) in Linux/Unix systems. mkdir stands for "make directory" and is essential for organizing files and creating new storage locations.

Quick Reference

Command Name:

mkdir

Category:

file management

Platform:

linux

Basic Usage:

mkdir new_directory

Common Use Cases

  • 1

    Directory creation

    Create new directories in the filesystem

  • 2

    File organization

    Organize files and directories hierarchically

  • 3

    Project setup

    Create project directories for new projects

  • 4

    Temporary directories

    Create temporary directories for temporary files

Syntax

mkdir [OPTION]... DIRECTORY...

Options

Option Description
-m, --mode=MODE Set file mode (permissions) as in chmod (default: 0777)
-p, --parents Create parent directories as needed, no error if existing
-v, --verbose Print a message for each created directory
-Z Set SELinux security context of each created directory to default type
--context[=CTX] Like -Z, or if CTX is specified, set the SELinux or SMACK security context to CTX
--help Display help information and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the mkdir command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

mkdir documents

Creates a new directory named documents in the current working directory. This is the most basic usage of mkdir.

mkdir dir1 dir2 dir3

Creates multiple directories at once. This example creates three directories named dir1, dir2, and dir3 in the current location.

mkdir -p projects/website/html

Creates nested directories in a single command. The -p option creates parent directories if they don't exist. This command creates projects, then website inside it, then html inside that.

mkdir -m 755 secure_folder

Creates a directory with specific permissions. This command creates secure_folder with permissions set to 755 (rwxr-xr-x), which gives read, write, and execute permissions to the owner, and read and execute permissions to group and others.

Advanced Examples:

mkdir -p -v project/{src,docs,tests}/{main,lib}

Creates a complex directory structure using brace expansion. This creates a project directory with subdirectories for source code, documentation, and tests, each having main and lib subdirectories. The -v option provides verbose output showing each directory as it's created.

mkdir -p "My Photos/Vacation 2023"

Creates a directory with spaces in its name. When directory names contain spaces, they should be enclosed in quotes to be properly interpreted.

mkdir --mode=u=rwx,g=rx,o= confidential

Creates a directory with symbolic mode permissions. This creates a directory where the owner has read, write, and execute permissions, the group has read and execute permissions, and others have no permissions.

mkdir -p -v "$(date +%Y-%m-%d)/logs"

Creates a directory with the current date as the name. This uses command substitution to create a directory with today's date in YYYY-MM-DD format, with a logs subdirectory.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Common Use Cases:

  • Creating project directories and file organization structures
  • Setting up application folder hierarchies
  • Creating dated backup directories
  • Establishing directory structures with specific permissions
  • Organizing files by categories or projects

Tips:

  • The -p option is very useful for creating complex directory structures in a single command
  • Combine with brace expansion (e.g., mkdir -p project/{src,lib,test}) to create multiple directories with similar paths
  • Use quotes for directory names with spaces or special characters
  • Remember that mkdir will fail if the target directory already exists (unless using -p)
  • Setting permissions with -m is more efficient than creating first, then using chmod
  • When creating directories for applications, consider using appropriate permissions for security

Related Commands:

  • rmdir - Remove empty directories
  • rm -r - Remove directories and their contents recursively
  • cd - Change directory
  • pwd - Print working directory
  • ls - List directory contents

Tips & Tricks

1

Use the -p option to create parent directories as needed

2

Use the -m mode option to set the permission mode

3

Use the -v option to display a message for each created directory

4

Use the -Z option to set the SELinux security context

5

Use the -Z option to set the ACL (Access Control List) entries

Common Use Cases

Directory creation

Create new directories in the filesystem

File organization

Organize files and directories hierarchically

Project setup

Create project directories for new projects

Temporary directories

Create temporary directories for temporary files

Scripting

Use in shell scripts to create directories programmatically

Related Commands

These commands are frequently used alongside mkdir or serve similar purposes:

Use Cases

1

Directory creation

Create new directories in the filesystem

2

File organization

Organize files and directories hierarchically

3

Project setup

Create project directories for new projects

4

Temporary directories

Create temporary directories for temporary files

5

Scripting

Use in shell scripts to create directories programmatically

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the mkdir command works in different scenarios.

$ mkdir
View All Commands