Bash mkdir Command – Create Directories

Imagine you are starting a fresh project, downloading a plethora of files, or just trying to keep your digital space neat. What is the initial step to organization? Making new spaces for your stuff! In the Linux terminal, that vital task belongs to the simple yet robust mkdir command.

This post will serve as your comprehensive guide to mkdir, shedding light on its core functions, valuable options, and practical applications. Think of it like constructing new rooms in a house, adding compartments to a filing cabinet, or preparing new empty containers to categorize your belongings. This guide is crafted for anyone new to Linux, those learning shell scripting, or users aiming to refine their command-line organization skills.

What is mkdir?

mkdir is an abbreviation for “make directory.” It is a foundational command-line utility in Linux and Unix-like operating systems.

Its primary function is to create one or more new directories (often referred to as folders) at specified locations within the file system. These new directories are always empty when first made. The capacity of mkdir to assist in maintaining an organized and easily navigable file system is indispensable for effective work and script execution.

Basic Usage of mkdir

Let is dive into the common ways you will use mkdir.

Creating a Single Directory in the Current Location

This is the most direct use.

Command:

mkdir my_new_folder

Explanation: This command will create a new directory named my_new_folder in your present working directory.

Creating Multiple Directories at Once

You can generate several directories simultaneously.

Command:

mkdir folder1 folder2 folder3

Explanation: This will create three distinct directories—folder1, folder2, and folder3—all within your current location.

Creating a Directory at a Specific Absolute Path

An absolute path begins from the root of your file system (/) and specifies the exact location regardless of where you currently are.

Command:

mkdir /home/your_username/documents/reports

Explanation: This command creates a reports directory inside /home/your_username/documents. It is helpful for creating folders far from your current spot.

Creating a Directory at a Specific Relative Path

Relative paths are defined in relation to your current working directory. They are shorter but rely on your starting point.

Command 1:

mkdir project_alpha/src

Explanation: If project_alpha exists in your current directory, this creates a src directory inside it.

Command 2:

mkdir ../my_sibling_folder

Explanation: This command creates my_sibling_folder one level up from your current directory, effectively making it a sibling to your current location.

Understanding mkdir Options (Flags)

mkdir offers several options, also known as flags, that modify its behavior. Let us explore the most useful ones.

-p (Parents)

This is arguably the most crucial mkdir option.

Explanation: If you attempt to create a directory deep within a non-existent path (e.g., a/b/c where a does not exist), mkdir will typically fail. The -p option tells mkdir to create any necessary parent directories along the way. Additionally, it won’t produce an error if the directory you’re trying to create already exists, which is convenient for scripting.

Example:

mkdir -p project/src/main/java

Contrast with:

mkdir project/src/main/java 
# This would fail with "No such file or directory" if 'project' didn't exist 

Using -p ensures all intermediate directories (project, src, main) are created if they don’t exist, before finally creating java.

-v (Verbose)

Explanation: When you use the -v flag, mkdir will print a message for every directory it successfully creates. This is incredibly helpful for confirmation, especially when making multiple or nested directories.

Example:

mkdir -v new_docs 
mkdir -pv images/thumbnails

Expected Output (similar to):

mkdir: created directory 'new_docs' 
mkdir: created directory 'images' 
mkdir: created directory 'images/thumbnails' 

Notice how -pv combines the benefits of both flags, creating parents quietly but announcing each directory as it’s made.

-m (Mode)

Explanation: This option allows you to set specific file permissions (mode) for the new directory as it’s being created. Permissions are often represented as octal numbers (e.g., 755).

Example:

mkdir -m 755 confidential_data

Brief explanation of 755:

  • The first 7 (read, write, execute) applies to the owner of the directory.
  • The second 5 (read, execute) applies to the group associated with the directory.
  • The third 5 (read, execute) applies to others (everyone else).
A common permission is 755, meaning the owner has full control, while group members and others can read and execute (traverse) the directory. Be cautious with 777, as it grants full permissions to everyone, which can be a security risk.

Practical mkdir Scenarios and Examples

Let us put mkdir to work with some real-world examples.

Scenario 1: Setting Up a New Project Structure Rapidly

You are kicking off a web development project and require standard folders for HTML, CSS, JS, and images.

Command:

mkdir -p my_website/{html,css,js,images}

Explanation: This uses brace expansion to efficiently create the my_website directory and its four immediate subdirectories in one go. The -p flag ensures my_website is created if it does not exist.

Scenario 2: Creating a Dated Log Directory

You want a new directory for logs, named with today’s date.

Command:

mkdir "logs_$(date +%Y-%m-%d)"

Explanation: Command substitution ($(...)) executes the date command and inserts its output (e.g., 2025-07-24) directly into the mkdir command, creating a directory like logs_2025-07-24. The quotes are important in case the date output included spaces (though in this format, it would not).

Scenario 3: Organizing Temporary Files

You need a temporary workspace for some files.

Command:

mkdir /tmp/my_temp_space

Explanation: /tmp is a common, standardized location for temporary files on Linux systems. Remember that contents of /tmp are often cleared upon system reboot, so don’t store critical information here long-term.

Scenario 4: Creating a Hidden Directory for Configuration

You want a directory for application configurations that does not displayed in the ls output (hidden) by default.

Command:

mkdir .my_app_configs

Explanation: In Linux, any file or directory name starting with a dot (.) is considered “hidden” by default. It would not show up with a regular ls command; you’d need ls -a to see it.

Scenario 5: Creating Directories with Spaces (and Best Practices)

You need a directory named My Project Files.

Method 1 (Using Quotes – Recommended):

Command:

mkdir "My Project Files"

Explanation: Enclosing the entire name in double quotes tells the shell to treat the spaces as part of the directory name. This is generally the safest and clearest method.

Method 2 (Escaping Spaces):

Command:

mkdir My\ Project\ Files

Explanation: The backslash (\) before each space “escapes” it, telling the shell to interpret the space literally rather than as a command separator.

Best Practice Tip: While possible, it’s generally best to avoid spaces in directory names for easier command-line usage and scripting. Use underscores (my_project_files) or hyphens (my-project-files) instead.

Scenario 6: Verifying Directory Creation

After running mkdir, always verify it worked as expected.

  • Workflow:
    1. mkdir new_stuff
    2. ls (to see new_stuff listed)
    3. ls -l (to see new_stuff with permissions, indicating it is a directory with a d at the beginning of the permissions string)

Scenario 7: Using mkdir in a Simple Script

mkdir is a workhorse in shell scripts for automated organization. Imagine a script that sets up a new user’s project directory.

#!/bin/bash

read -p "Enter new project name: " PROJECT_NAME

# Create the main project directory, -p ensures it is created if it does not exist, -v gives verbose output
mkdir -pv "$PROJECT_NAME"

# Create standard subdirectories within the project
mkdir -p "$PROJECT_NAME/docs" "$PROJECT_NAME/src" "$PROJECT_NAME/tests"

echo "Project '$PROJECT_NAME' structure created successfully!"

Explanation: This script prompts the user for a project name, then uses mkdir -pv to create the main project folder. Afterward, it uses mkdir -p with multiple arguments to create the docs, src, and tests subdirectories inside the new project folder.

Common Pitfalls and Tips

Here are a few things to keep in mind to make your mkdir experience smoother.

  • Permission Denied: If you try to create a directory in a location where your current user does not have write permissions (e.g., directly under /usr or /var/www without sudo), mkdir will give you a “Permission denied” error. Always check your current directory’s permissions using ls -ld . or use sudo if necessary and appropriate.
  • “File exists” / “Directory exists”: If you try to create a directory name that already exists without the -p flag, mkdir will inform you “File exists” or “Directory exists” and would not do anything. However, if you use mkdir -p and the target directory already exists, it will simply do nothing and exit successfully without an error message, which is quite useful for idempotent operations in scripts.
  • Naming Conventions: Sticking to a consistent naming convention is crucial for an organized and readable file system.
    • Avoid spaces (as shown above).
    • Prefer lowercase.
    • Use hyphens (-) or underscores (_) for separators (e.g., my-project or my_project).
    • Avoid most special characters as they can have specific meanings in the shell and lead to unexpected behavior.
  • Tab Completion: While not directly for mkdir, remember that Tab completion is your best companion when navigating into the directories you just created (using cd) or listing their contents (ls). It saves time and prevents typos!

By now, you should feel comfortable wielding the mkdir command to create, organize, and manage your directories in Linux. It is a foundational skill that will significantly enhance your command-line efficiency and help you maintain a clean and structured digital workspace. Keep experimenting, and you will become a directory-making pro in no time!

What is the next command-line tool you are curious about mastering?

FAQ’s – Linux/Unix Bash Command – mkdir


What is the mkdir command in Bash?
The mkdir (make directory) command is used to create new directories (folders) in Unix-like operating systems. You can create single or multiple directories at once using this command.


How do I create a new directory using mkdir?
To create a directory named myfolder, run:

mkdir myfolder

This creates a new folder named myfolder in the current working directory.


How do I create multiple directories at once?
You can list multiple directory names:

mkdir dir1 dir2 dir3

This creates dir1, dir2, and dir3 at once.


How do I create a directory with a path that includes parent folders?
Use the -p option to create parent directories if they don’t exist:

mkdir -p parent/child/grandchild

If parent or child don’t exist, they will be created automatically.


What happens if I use mkdir on a directory that already exists?
Without any options, mkdir returns an error:

mkdir myfolder

Output:

mkdir: cannot create directory ‘myfolder’: File exists

To suppress the error, use the -p option:

mkdir -p myfolder

How do I check if a directory was created successfully?
You can check the return status of mkdir:

mkdir myfolder
if [ $? -eq 0 ]; then
  echo "Directory created"
else
  echo "Failed to create directory"
fi

Or check using ls or test:

[ -d myfolder ] && echo "Directory exists"

How do I set permissions when creating a directory?
Use the -m option followed by a permission mode (like 755):

mkdir -m 755 myfolder

This sets rwxr-xr-x permissions for the new directory.


How can I see what permissions a new directory has after creation?
Use ls -ld to view details:

ls -ld myfolder

Output example:

drwxr-xr-x  2 user group 4096 Jul 19 12:00 myfolder

How do I create a directory with a space in the name?
Use quotes or escape the space:

mkdir "My Folder"
# or
mkdir My\ Folder

How do I create nested directories using a variable in Bash?
Assign a path to a variable and use mkdir:

folder="dir1/dir2"
mkdir -p "$folder"

This will create both dir1 and dir2 if they do not exist.


Can I create directories in another user’s home directory?
Only if you have appropriate permissions. For example:

mkdir /home/otheruser/newfolder

If you do not have permission, use sudo (with caution):

sudo mkdir /home/otheruser/newfolder

How do I create a temporary directory using mkdir?
You can use mktemp for generating a unique name, and mkdir to create it:

tmpdir=$(mktemp -d)
mkdir "$tmpdir"
echo "Temporary directory created at $tmpdir"

Or just use:

mktemp -d

This creates and returns the path of a new temp directory.


How do I create a directory and navigate into it in a single command?
You can chain the commands using &&:

mkdir newfolder && cd newfolder

This ensures you only cd if mkdir is successful.


What is the difference between mkdir and mkdir -p?

  • mkdir: Fails if the parent directory does not exist.
  • mkdir -p: Creates the full directory path including any missing parents and suppresses errors if the directory already exists.

Can I create a directory with special characters in the name?
Yes, but you must quote the name:

mkdir "folder-with-@-symbol"

Avoid using characters that have special meaning in the shell (e.g., *, ?, /).


How can I create directories from a list in a file?
Use a loop:

while read dirname; do
  mkdir -p "$dirname"
done < dir_list.txt

Each line in dir_list.txt should contain a directory path.


What does the return value of mkdir indicate?

  • 0: Directory created successfully
  • 1: Error occurred (e.g., permission denied, already exists)

You can check it with $?:

mkdir myfolder
echo $?

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment