mkdir command in linux: 10 Essential Wins for Developers

mkdir command in linux

mkdir command in linux is the foundation of file organization,Linux users use the mkdir command to create directories for organizing files on the computer’s filesystem.

The purpose of this article is to show you more than 10 easy ways to use the mkdir command, so you can avoid problems caused by small mistakes in file or folder paths.

You will learn everything from basic folder creation to how to create nested directory structures, assign security permissions in real-time, and troubleshoot permissions errors with ease.

Basic syntax

Knowing how to write mkdir command will help you avoid small mistakes that can stop or slow down your work.

The command syntax for mkdir is an easy-to-understand format: mkdir [options] directory_name.

  • You can enter multiple directory names as your input.
  • mkdir offers optional flags, such as permission settings and the creation of a parent directory, that change how the command operates.
  • You may run this command on any Linux distribution, including Ubuntu, CentOS, and Arch.
  • If you attempt to create a directory with illegal characters or spaces in the directory name, you will receive error from the command.

The abbreviation mkdir means “make directory,” and its primary purpose is to allow you to create a space for a new folder within your file system.

At a minimum, you must provide a name when creating folders with the mkdir command; however, be aware that the mkdir command creates directories in the directory where your terminal session is currently placed. If you would like to create a directory elsewhere, you must use an absolute path.

It is best practice to avoid spaces in folder names, using underscores or hyphens instead, to make future scripting much easier and less prone to breakage.

Create a single directory

The command mkdir (make directory) allows you to create an empty folder in the current working directory (wherever you have your terminal open). So to create an empty “backup” folder in your working directory, just type mkdir backup.

  • If this command does not return any messages after executing, then it has successfully created the empty backup folder.
  • If you want to include spaces in the name of your new folders, such as “My Documents” or “My Downloads”, use double quotes around the folder name when entering the mkdir command; for example, mkdir “My Documents”.
  • If you’re using an absolute path instead of a relative path, you can create folders outside of your current system folder. For example, you could enter the command mkdir ~/Documents/Backup to create a folder named Backup within your home folder.

Create multiple directories at once

Efficiency is key when setting up a new project environment. The mkdir command allows you to spawn dozens of folders in a single second, saving you from repetitive typing.

  • Creates several independent folders with one command: mkdir dir1 dir2 dir3.
  • Uses brace expansion for professional patterns like mkdir folder_{1..10}.
  • Saves time when setting up standardized project structures for different clients.
  • Reduces the risk of human error compared to creating folders one by one.
  • Works seamlessly with shell variables for automated folder generation.

Imagine you need to create folders for the twelve months of the year. Instead of running the command twelve times, you can simply type mkdir jan feb mar apr may jun jul aug sep oct nov dec.

For even more power, use curly braces. Typing mkdir project_{alpha,beta,gamma} will result in three folders named project_alpha, project_beta, and project_gamma immediately appearing in your directory.

This feature of is a lifesaver for developers who need to generate sequential data storage. Running mkdir backup_{01..50} creates fifty numbered folders in a heartbeat, ready for your data archives.

Create nested directory structures with -p

The -p flag is the most powerful, allows you to create an entire file path without having any of the individual directories first.

When you use the -p option with the mkdir command, it eliminates potential errors within your scripts that would occur due to missing or empty directories.

You can easily create an entire range of folders using the mkdir command with the -p flag, just like you would with the dev/src/lib/assets example.

  • The command mkdir -p will create any folders that are specified in the entire path structure and, if any of those folders already exist, it does not give you an error for trying to create a folder that already exists.

Set permissions at creation time with -m

Afterthoughts should never be used to secure; lock down or open up the folder right when it is created using this command, thereby closing the security gap.

  • Establish specific access levels by using mkdir -m 700 private_folder.
  • Bypasses default system settings for ‘umask’ for certain directories that require extra security.
  • Establish a safe way to create public-facing folders with mkdir -m 755 public_html.
  • Combine with the -p flag to set permissions for the entire nested path.
  • Reduce the number of commands required to secure your server file system.

Instead of creating a directory and then using chmod, create the directory all in one step using the command mkdir -m 400 secret_keys, which creates a directory that is readable only by the owner, thus providing immediate protection for sensitive information.

The numbers used with the -m flag are in octal format.

The first digit of the mkdir command’s octal format ‘700’ indicates the owner has full control of the directory; no other users on the system will have any access whatsoever.

Use verbose mode -v to see what is created

When automating complex tasks, it is important to give lots of feedback to both the human operator and software systems.

This command has a Verbose Switch which can be used to confirm to the human operator exactly how the mkdir command is progressing.

In addition to being useful in debugging shell scripts by displaying what folders were created successfully, it is also beneficial when using a loop to create hundreds of folders because it can show you that the loop is functioning correctly.

Using the Verbose Switch can be very helpful if you are creating a new directory structure and want to see how many directories were created successfully from the command line.

The Verbose Switch will provide you with peace of mind when connecting to a remote server via SSH and configuring it.

  • When you type mkdir -v new_project, you will receive an explicit confirmation message indicating success.
  • It will help prevent confusion when creating folders on a slow network drive.
  • A professional deployment script that creates a complete application directory structure will use mkdir -pv app/{bin,config,data} to generate a comprehensive list of everything created.
  • This can provide an easy way to identify issues later if something goes wrong.

With the Verbose Switch, the mkdir command can be thought of as your “status monitor” on your terminal.

The Verbose Switch makes a task that is usually a silent operation visible and allows you to monitor the operating progress in real time.

Common errors and fixes

The fastest way to get back to work without frustration is to understand what is causing an error with this command and how to resolve it.

  • If you get an error message that starts with “Permission Denied” this means that you do not have permission to write to that directory. In order to gain administrator privileges you can use the command “sudo mkdir” to create a new directory under root privilege.
  • If you get the error message “File Exists” this means that the directory or file already exists in that location. To resolve this issue you will need to either review the path for accurate information or use “-p” to ignore any existing folders when creating a new directory.
  • If you receive “No such file or directory” error message this means that there are missing parent folders in the given directory. The error message can be resolved by adding the “-p” option to create all of the missing parent folders as well.
  • In the event of a “Read-only file system” message, a possible hardware issue or the drive needs to be re-mounted. You may need to check for your operating system’s TOS to determine if digital signatures are present.
  • If you get the “Disk quota exceeded” error message then you will have to delete any old files before you can create any new folders in that directory.

The most frequently encountered error message is “Permission Denied.”

This typically occurs when you attempt to create a directory within a protected system folder like “/etc.”

To bypass this restriction, you can utilize the command “sudo mkdir /etc/my_config,” which empowers the mkdir utility with “root” capabilities.

If you receive a “File Exists” error message this denotes that another item (either a folder or file) has already claimed that name.

In Linux, multiple files and folders cannot share the same name in the same location.

By understanding these error messages you will not view the “mkdir” command as a source of issues; rather, you will view it as an intelligent resource for identifying your issues.

Practical project and log structures

Theoretical knowledge is indeed beneficial, but practical experience is always going to prove more valuable.

This command is essentially how developers create a consistent environment to code and log their work.

  • Using mkdir to generate a new website project structure: mkdir -p website/{css,js,img,vendor}.
  • Using mkdir to create a new folder structure that will help keep logging organized: mkdir -p /var/log/app/2026/{01..12}.
  • Using mkdir this way to create a secure backup of your database: mkdir -m 700 ~/backups/database.
  • Using mkdir to set up a data science workspace: mkdir -p research/{data,notebooks,results}.
  • Using mkdir to create a structure to store all of the user uploads: mkdir -p uploads/user_{001..100}.

Using this command will help you avoid crashing your programme by attempting to save a log file inside a non-existent folder.

If you run the command mkdir -p logs/$(date +%Y/%m/%d), it will create a folder for the current year and then create a folder for the month followed by the day.

This way, you will have a very easy-to-follow structure when viewing your logs.

Combining mkdir with other commands

The terminal is similar to a set of LEGO bricks.

You can make mkdir (MAKE DIRECTORY) in Linux in many different ways by combining multiple programs with it to accomplish more complicated things with less typing than if you wrote separate lines for each command.

Examples of using mkdir with other commands are:

  • make the directory and go to it at the same time: mkdir test_dir && cd test_dir
  • make a directory and set the ownership of it: sudo mkdir dir && sudo chown user:user dir
  • find all empty directories and recreate them in another location with find and mkdir
  • create a directory and create an empty file in that directory: mkdir config && touch config/settings.conf
  • create and populate a directory by looping through a text file of file names with mkdir

Making a directory then moving to it is a common pattern of workflow.

Instead of using two commands in sequence, you can make an alias in bash or chain the commands together: mkdir -p new_app && cd new_app.

This allows you to keep the same workflow without any interruption.

Additionally, mkdir is one of the most used commands in automation scripts in Linux.

An example of an automation script that leverages mkdir is a script that takes a text file of usernames and creates a home directory for each of them with a for loop and mkdir.

This ability to integrate with other commands is why mkdir is such a critical command in Linux.

Rather than being an isolated command, mkdir is a foundational part of creating the entire Linux environment.

Best practices for using mkdir

To become a master of Linux you should learn from the habits of experienced Linux Users.

The following the Mkdir command best practices will help you avoid 99% of future path issues.

  • In a script, Always use the -P option to prevent “directory exists” or “path not found” errors.
  • Never use spaces, special characters or Emoji icons in your directory names.
  • Always use lower-case letters to ensure consistency on different filesystems.
  • Instead of naming a directory “folder1” give it a name like “archive_2016.”
  • Before you run the Mkdir command double-check where you are by running PWD.

If you run “Mkdir Project_Data” on one server and then “Mkdir project_data” on another server, when you’re moving Directory files between the two Systems you’ll have a case sensitivity problem.

The Mkdir command is your primary tool for Maintaining “directory hygiene.”

Properly Named and organized Folders will make your Linux Experience faster, safer and easier to enjoy.

mkdir command in linux:Your Questions Answered

Q: What does mkdir command do?

It creates new directories (folders) in the filesystem, either one at a time or in batches, using different options for permissions and nesting.

Q: How do I create a folder with a space in its name using mkdir command?

You must wrap the name in quotes, like mkdir "New Folder", or use a backslash to escape the space: mkdir New\ Folder.

Q: How can I create nested folders like a/b/c with mkdir command ?

Use the -p (parents) flag: mkdir -p a/b/c. This creates any missing folders in the path automatically.

Q: What happens if I try to create a folder that already exists with mkdir command in linux?

The command will show an error message: “File exists”. However, if you use the -p flag, it will ignore the error and do nothing.

Q: How do I set permissions while creating a directory with mkdir command in linux?

Use the -m flag followed by the octal mode, for example: mkdir -m 755 web_folder to set specific read/write/execute rights.

Q: Can I create multiple folders at once with mkdir command in linux?

Yes, simply list the folder names separated by spaces: mkdir dir1 dir2 dir3.

Q: How do I see a confirmation message for every folder created by mkdir command in linux?

Use the verbose flag -v. Running mkdir -v my_folder will print a message confirming the folder was created.

Q: Why do I get “Permission denied” when using mkdir command in linux?

You are likely trying to create a folder in a protected system area. Use sudo mkdir to run the command with administrator privileges.

Q: How do I create folders numbered 1 to 100 with mkdir command in linux?

Use brace expansion: mkdir folder_{1..100}. This creates all 100 directories in a split second.

Q: Does mkdir command in linux work on hidden folders?

Yes, any folder name starting with a dot, like mkdir .config, will be created as a hidden directory.

Q: What is the difference between mkdir and md in Linux?

In most Linux shells, md is not a standard command. You should always use the full mkdir command in linux for compatibility.

Q: Can I use mkdir command in linux to create a folder on a remote server?

Yes, you can run it over SSH: ssh user@server "mkdir new_folder".

Q: How do I create a directory structure for a new Python project with mkdir command in linux?

A common way is mkdir -p project_name/{src,tests,docs,data}.

Q: How can I use the date in a folder name with mkdir command in linux?

Use command substitution: mkdir backup_$(date +%F) to create a folder with the current date (e.g., backup_2026-01-01).

Q: Does mkdir command in linux create files too?

No, it only creates directories. To create an empty file, you should use the touch command instead.

Q: How do I create a folder with the same name as a file using mkdir command in linux?

You cannot. Linux does not allow a file and a directory to have the exact same name in the same location.

Q: How do I check if a directory was successfully created by mkdir command in linux?

You can use the ls command to list the contents or check the exit status with echo $? (0 means success).

Q: Can I use mkdir command in linux to set the group ownership?

No, mkdir only sets permissions. You must use the chown or chgrp command afterward to change ownership.

Q: What is the maximum length of a directory name for mkdir command in linux?

On most Linux systems (using Ext4), the maximum length for a single directory name is 255 characters.

Q: How do I help a script continue even if mkdir command in linux fails?

Using mkdir -p is the best way, as it won’t stop the script if the directory already exists.

Mastering the mkdir command in linux is a vital skill that transforms how you interact with your computer. You have learned how to create single folders, build massive nested directory trees with the -p flag, and secure your data right at the moment of creation using the -m flag. These techniques are essential for keeping your server logs, development projects, and system backups organized and error-free.

You now have the confidence to manage any directory structure on a Linux system, from a simple home computer to a high-stakes production server. By following the best practices of naming and permission management, you will avoid the common pitfalls that slow down other developers. Continue exploring the terminal to further build your expertise and streamline your digital workflow.

Learn more Linux & cybersecurity: CodingJourney.co.in | CodingJourney Sulekha


🚀 Basic Linux Commands – Master Terminal in 5 Minutes!

Leave a Reply

Your email address will not be published. Required fields are marked *