Skip to Content

1. Introduction

Organizing files effectively is crucial when working on any computer system. In Linux, directories (often called folders) form the hierarchical structure used to store and manage files. The mkdir command is the standard utility used to create new directories from the command line.

mkdir stands for make directory. Mastering this command is essential for basic filesystem management.

2. Basic Syntax

The fundamental structure of the mkdir command is:

# Create one or more directories $ mkdir [OPTIONS] directory_name...
  • [OPTIONS]: Optional flags that modify the command’s default behavior, such as creating parent directories or setting permissions.
  • directory_name...: One or more names for the directories to create, specified as:
    • Relative Path: Relative to the current working directory (e.g., new_folder, ../backup).
    • Absolute Path: Starting from the root directory (e.g., /tmp/my_app_data).

3. Basic Usage and Examples

3.1 Creating a Single Directory in the Current Location

This creates a directory named project_alpha in the current working directory.

# Check the current working directory $ pwd /home/user # Create the directory $ mkdir project_alpha # Verify creation $ ls -ld project_alpha drwxr-xr-x 2 user user 4096 May 3 16:00 project_alpha/

3.2 Creating Multiple Directories Simultaneously

You can create multiple directories at the same level by listing their names separated by spaces.

# Check the current working directory $ pwd /home/user/project_alpha # Create multiple subdirectories $ mkdir src include assets build # Verify creation $ ls assets build include src

3.3 Creating a Directory Using a Path

3.3.1 Relative Path

Creates a directory within an existing directory relative to the current location.

# Check the current working directory $ pwd /home/user # Create 'config' inside the existing 'project_alpha' directory $ mkdir project_alpha/config # Verify creation $ ls project_alpha/ assets build config include src

3.3.2 Absolute Path

Creates a directory at a specific location starting from the root (/), regardless of the current directory. This may require elevated privileges (sudo) outside the user’s home directory.

# Create a temporary directory in /tmp $ mkdir /tmp/my_temp_files # Verify creation $ ls -d /tmp/my_temp_files /tmp/my_temp_files # Create a directory in /opt (may require sudo) $ sudo mkdir /opt/my_new_app

4. Important mkdir Options

Options enhance the mkdir command’s functionality for specific use cases.

4.1 -p or --parents: Create Parent Directories as Needed

Purpose: Allows creation of nested directories by automatically creating any missing parent directories. Without -p, mkdir fails if parent directories do not exist. It also suppresses errors if the target directory already exists.

Syntax: mkdir -p path/to/nested/directory

Example:

# Try creating nested directories without -p (fails if 'reports' doesn't exist) $ mkdir reports/2025/q1 mkdir: cannot create directory ‘reports/2025/q1’: No such file or directory # Use -p to create the entire structure $ mkdir -p reports/2025/q1 # Verify the structure $ ls -R reports/ reports/: 2025 reports/2025: q1 reports/2025/q1: # Re-run with -p (no error if directories exist) $ mkdir -p reports/2025/q1

4.2 -m MODE or --mode=MODE: Set Permissions at Creation

Purpose: Sets specific permissions for the new directory using octal (e.g., 755, 700) or symbolic notation (e.g., u=rwx,go=rx). Without -m, permissions are set based on the system’s umask.

Syntax: mkdir -m OCTAL_MODE directory_name or mkdir --mode=SYMBOLIC_MODE directory_name

Example (using octal): Create a shared_folder accessible by owner and group (775).

# Create directory with specific permissions $ mkdir -m 775 shared_folder # Verify permissions $ ls -ld shared_folder drwxrwxr-x 2 user user 4096 May 3 16:10 shared_folder/

Example (using symbolic): Create private_data accessible only by the owner.

# Create directory with symbolic permissions $ mkdir --mode=u=rwx,go= private_data # Alternatively: $ mkdir -m 700 private_data # Verify permissions $ ls -ld private_data drwx------ 2 user user 4096 May 3 16:11 private_data/

4.3 -v or --verbose: Print a Message for Each Created Directory

Purpose: Outputs a confirmation message for each directory created, useful for tracking actions, especially with -p for nested paths.

Syntax: mkdir -v directory_name... or mkdir -pv nested/path

Example:

# Create directories with verbose output $ mkdir -pv backups/daily backups/weekly backups/monthly mkdir: created directory 'backups' mkdir: created directory 'backups/daily' mkdir: created directory 'backups/weekly' mkdir: created directory 'backups/monthly'

5. Important Considerations

  • Permissions Required: You need write (w) permission on the parent directory to create a new directory within it.
  • Existing Directories: Without -p, attempting to create an existing directory results in an error (e.g., mkdir: cannot create directory ‘project_alpha’: File exists). The -p option avoids this error.
  • Naming Conventions: Avoid spaces and special characters (e.g., *, ?, &, ;, |, <, >) in directory names to prevent shell interpretation issues. Use underscores (_) or hyphens (-) instead.
  • Handling Spaces in Names: For directory names with spaces, use quotes or escape the spaces:
# Create directory with spaces $ mkdir "My Important Documents" # OR $ mkdir My\ Important\ Documents

6. Frequently Asked Questions (FAQ)

6.1 How do I create multiple nested directories at once?

Use the -p option to create nested directories, ensuring all parent directories are created if they don’t exist.

# Create nested directories $ mkdir -p level1/level2/level3

6.2 What permissions does mkdir use by default?

Default permissions depend on the system’s umask, which subtracts from the base permissions (777 for directories). A common umask of 022 results in 755 (rwxr-xr-x). Check your umask with:

# Check umask $ umask 0022

Override default permissions using the -m option.

6.3 Can I create a directory and set its owner/group at the same time?

No, mkdir does not support setting owner or group directly. Use chown or chgrp after creating the directory.

# Create directory and change ownership $ mkdir new_dir $ sudo chown specific_user:specific_group new_dir

6.4 What happens if I try to create a directory that already exists?

Without -p, mkdir fails with an error:

# Attempt to create existing directory $ mkdir project_alpha mkdir: cannot create directory ‘project_alpha’: File exists

With -p, no error is produced, and the command ensures the directory exists.

# Create directory with -p (no error if exists) $ mkdir -p project_alpha

6.5 Is mkdir atomic? (Regarding concurrent operations)

The mkdir() system call is generally atomic on POSIX-compliant systems, ensuring only one process succeeds if multiple try to create the same directory simultaneously. However, with -p, creating multiple parent directories is not guaranteed to be atomic as a whole.

7. Conclusion

The mkdir command is a cornerstone of Linux filesystem management. Its simplicity belies its power, especially when using options like -p for nested directories and -m for custom permissions. By understanding paths, permissions, and naming conventions, users can efficiently organize their filesystem. This guide provides practical examples to help both beginners and advanced users leverage mkdir effectively.

8. mkdir Command: Reference Table of Key Options

(Make directories)

Option(s)DescriptionExample CommandUse Case
-p, --parentsCreate parent directories as needed, no error if existingmkdir -p dir1/dir2/dir3Create nested directory structures easily
-m MODE, --mode=MODESet file mode (permissions) on creationmkdir -m 700 private_dirCreate a directory with specific initial permissions
-v, --verbosePrint a message for each created directorymkdir -v logs reportsGet confirmation of which directories were actually created
-Z / --contextSet SELinux security contextmkdir -Z shared_dirApply SELinux context during creation (SELinux systems only)