How to Create a Directory in Linux Using mkdir Command [5 best Methods]

Written by

Reviewed by

Last updated: June 23, 2024

Expert verified

SVG Image

TL;DR

To create a directory in Linux using mkdir command, you can try these methods:

  1. To create a single directory, use the command mkdir documents in the terminal.
  2. For creating multiple directories at once, run mkdir photos videos documents.
  3. To create nested directories, use the -p option with mkdir -p parent/child.

Continue reading the guide below to learn to create a directory in Linux using mkdir command and the common errors that can occur while using the command.


Keeping your files organized on Linux can make a big difference, just like using folders for documents, pictures, or music on your computer. The mkdir command in Linux is your go-to tool for this. In this post, I’ll show you how to use mkdir to create single and multiple directories, set permissions, and even add timestamps. You’ll also learn how to avoid common mistakes that can trip you up. By the end, you’ll have a solid grasp of managing directories efficiently and effectively.

What is the mkdir Command?

The mkdir command in Linux stands for make directory. It lets you create new directories (folders) on your file system. Think of it as a way to organize your files into different categories, just like creating folders on your computer to store documents, pictures, or music.

Importance in Unix-Based Systems

  • Organization: Helps users and administrators organize files neatly.
  • Efficiency: Saves time by quickly creating directories through simple commands.
  • Structure: Provides a clear, hierarchical structure for data storage.
  • Automation: Can be used in scripts to automate directory creation tasks.
  • Consistency: Ensures consistent directory structures across different systems and users.
  • Management: Facilitates easier management and navigation of the file system.
  • Security: Allows setting permissions and ownerships, enhancing file system security.

Basic Syntax of mkdir

The basic syntax to create a directory in Linux using mkdir command is:

arduinoCopy codemkdir [OPTION] DIRECTORY

Explanation of Syntax Components

  1. mkdir: The command itself.
  2. [OPTION]: Optional flags that change how the command works. These are not always needed.
  3. DIRECTORY: The name of the directory or directories you want to create.

How to Create a Directory in Linux Using mkdir Command

To create new directories using mkdir command in Linux, you have several powerful methods at your disposal. From creating single directories to managing nested directories and setting specific permissions, the mkdir command provides flexible options for organizing your files and folders. Additionally, you can incorporate timestamps into directory names for better version control.

1. Single Directories

Creating a single directory using the mkdir command is a straightforward method that allows you to quickly organize your files and folders with meaningful names, making it easier to locate and manage specific content. To create a directory in Linux using mkdir command, follow these steps:

  1. Open the Terminal.
opening-terminal-to-Create-New-Directories-Using-mkdir-Command
  1. Enter the following command to create a directory named documents:
mkdir documents
  1. Press Enter to execute the command.
creating directory

2. Multiple Directories

The ability to create multiple directories simultaneously with the mkdir command saves time and effort, making it ideal for situations where you need to create several related directories at once, such as setting up project folders or organizing different categories of files. To create directories in Linux, follow these steps:

  1. Launch the Terminal window and enter the mkdir command, followed by the names of the directories separated by spaces. For example, to create directories named photos, videos, and documents, use the following command:
mkdir photos videos documents
  1. The command will create multiple directories at the same time.
creating-multiple-directories

3. Nested Directories

The capability to create nested directories or subdirectories using the mkdir command enables you to establish a hierarchical structure for your files, providing a systematic organization that simplifies navigation and enhances overall file management. Follow these steps to create nested directories:

  1. Access the command window.
  2. Specify the parent directories along with the child directory names, separated by slashes (/). For example, to create a directory structure with a parent directory named parent and a child directory named child, use the following command:
mkdir -p parent/child
  1. The command will create a hierarchical structure of the directories.
creating-nested-directories

4. Directories with Different Permissions

By utilizing the mkdir command to create directories with specific permissions, you can ensure the security and integrity of your files, granting appropriate access levels to different users or groups, thus maintaining control over your directory structure. To set specific permissions while creating directories, follow these steps:

  1. Enter your command window.
  2. Enter the mkdir command, followed by the directory name and the desired permissions. For example, to create a directory named private with read and write permissions, use the following command:
mkdir -m 600 private
  1. The output will be:
creating-directories-with-specific-permissions

5. Directories with Timestamps

Incorporating timestamps into directory names using the mkdir command helps you track file changes and assists in version control, providing valuable information about the creation or modification times of files, allowing for better organization and historical references.Follow these steps to create directories with timestamps:

  1. Luanch the Terminal window and run the mkdir command, followed by the desired directory name, along with the timestamp. For example, to create a directory named backup_20230607, use the following command:
mkdir backup_20230607
  1. Press Enter to execute the command to create directory with Timestamps.
creating-directoy-with-timestamps

Common Mistakes to Avoid and Tips to Prevent Errors

Creating directories with the mkdir command is simple, but small mistakes can cause issues. Here are five common mistakes to avoid and tips to help you prevent errors.

  • ✏️ Misspelling Directory Names: Typing errors can lead to unexpected directory names or errors. Double-check your directory names before running the command to avoid these issues.
  • ⚙️ Forgetting the -p Option: Not using the -p option prevents mkdir from creating nested directories if the parent directories don’t exist. Always include the -p option when creating nested directories.
  • 🔒 Using Incorrect Permissions: Incorrect permissions can make directories inaccessible or insecure. Use the -m option to set appropriate permissions when creating directories.
  • 📂 Creating Duplicate Directories: Running the mkdir command with an existing directory name can cause errors or confusion. Check if a directory with the same name already exists to avoid duplication.
  • 🔍 Ignoring Case Sensitivity: Linux is case-sensitive, so Documents and documents are different directories. Be mindful of case sensitivity and be consistent with your naming conventions.

Create Directory in Linux: Final Thoughts

Creating directories using the mkdir command is straightforward once you get the hang of it. From making single and multiple directories to setting specific permissions and adding timestamps, these methods cover a lot.

  • Learn how to rename a directory in Linux, which is useful if you need to organize or update your directory structure.
  • Find out how to count files in Linux using the command line to manage and verify the contents of your directories efficiently.
  • Discover how to list only directories in Linux, making it easier to navigate and manage large file systems.

Frequently Asked Questions

Is it possible to create directories with special characters using mkdir?

Yes, it is possible to create directories with special characters using the mkdir command. However, it is important to exercise caution when using special characters, as they may have special meanings in the command line. To create directories with special characters, there are two approaches you can take. First, you can escape the special characters using a backslash (). For example, to create a directory named my_dir!, you would use the command mkdir my_dir\!. Alternatively, you can enclose the entire directory name within quotes. For example, mkdir “my_dir!” would create a directory with the name my_dir!.

How can I create directories with a specific owner or group using mkdir command?

While the mkdir command does not provide direct options to set the owner or group of directories during creation, you can easily change the owner or group after the directory has been created using the chown and chgrp commands. To change the owner of a directory, use the command chown followed by the desired user and the directory name. For example, chown john my_directory would change the owner of my_directory to the user john. To change the group, use the command chgrp followed by the desired group and the directory name. For example, chgrp staff my_directory would change the group of my_directory to staff.

What is the maximum length limit for directory names created with mkdir?

The maximum length limit for directory names created with mkdir depends on the file system being used. In most modern Linux distributions, the typical maximum length for a directory name is 255 characters. However, it is important to note that different file systems may have different limitations. When creating directory names, it is advisable to keep them concise and meaningful, while ensuring they do not exceed the maximum length limit of your file system. Exceeding the limit may lead to compatibility issues or errors when interacting with the directory and its contents.

Can the mkdir command be used to create directories on remote servers?

Yes, the mkdir command can be used to create directories on remote servers, provided you have appropriate access to the remote server. This can be achieved through the use of SSH (Secure Shell) or other remote access protocols. To create a directory on a remote server, establish a secure connection using SSH. Once connected, use the mkdir command in the remote Terminal to create directories as you would on a local machine. For example, ssh user@remote_server mkdir remote_directory would create a directory named remote_directory on the remote server. Ensure that you have the necessary permissions and access rights to create directories on the remote server before attempting to use the mkdir command remotely.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

3 Effective Methods To Select All in Vi/Vim

Next Post

5 Best Methods to Install .deb Ubuntu

Leave a Reply

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

Read next