Create New Directories Using mkdir Command [5 best Methods]

Written by

Reviewed by

Last updated: June 25, 2023

Expert verified

SVG Image

TL;DR

To create new directories using mkdir command, you can try these five methods:

  1. Easily create a single directory by using the mkdir command and specifying the desired name.
  2. Save time by using the mkdir command to create multiple directories simultaneously, just separate the names with spaces.
  3. Organize your files in a hierarchical structure by using the mkdir command with slashes to create nested directories.
  4. Set specific permissions while creating directories by using the mkdir command with the -m option and desired permissions.
  5. Keep track of file changes by incorporating timestamps into directory names using the mkdir command.

Common errors when using the mkdir command include “Permission Denied,” “Directory Already Exists,” “Invalid Characters in Directory Name,” and “Insufficient Disk Space.” Resolving these errors involves running the command with administrative privileges, ensuring unique directory names, using valid characters, and freeing up disk space.

Continue reading the guide below to learn to create new directories using mkdir command and the common errors that can occur while using the command.


The mkdir command, short for “make directory,” is a fundamental tool that allows you to create new directories effortlessly. With a few simple commands, you can create single directories, multiple directories, nested directories, and even directories with specific permissions. No more clicking through a graphical interface or typing lengthy commands manually! The mkdir command streamlines the directory creation process, saving you time and effort. In this article, I will explore the five types of directories that can be created using the mkdir command, explore advanced directory creation techniques, and common errors that can occur while using mkdir command.

How to Create New Directories 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 single directory, 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:
<strong>mkdir documents</strong>
  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 multiple directories simultaneously, 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:
<strong>mkdir photos videos documents</strong>
  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:
<strong>mkdir -p parent/child</strong>
  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:
<strong>mkdir -m 600 private</strong>
  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:
<strong>mkdir backup_20230607</strong>
  1. Press Enter to execute the command to create directory with Timestamps.
creating-directoy-with-timestamps

4 Common Errors When Using mkdir Command

When working with the mkdir command, there are a four common errors that users may encounter. By understanding these common errors and following the suggested resolutions, you can effectively troubleshoot issues when using the mkdir command and create directories without encountering any setbacks. Here are four common errors and suggestions for resolving them:

  • ๐Ÿ”’ Permission Denied: This error occurs when you don’t have the necessary permissions to create a directory in the specified location. It can happen if you are trying to create a directory in a system directory or a directory where you do not have write access. To resolve this issue, you can try running the mkdir command with administrative privileges using sudo, which will grant you the necessary permissions. Alternatively, choose a different location where you have write access, such as a directory within your user’s home directory.
  • ๐Ÿ”„ Directory Already Exists: If you attempt to create a directory with a name that already exists in the current location, the mkdir command will fail. This error often occurs when you unintentionally use the same name for a directory that already exists. To avoid this error, double-check the directory name you are specifying and ensure it is unique. If you want to create a directory with a similar name, consider adding a number or a descriptive term to make it distinct from the existing directory.
  • โŒ Invalid Characters in Directory Name: Certain characters are not allowed in directory names, such as slashes (/), colons (:), question marks (?), or other special characters that have a specific meaning in the command line. If you include these characters in the directory name, the mkdir command will throw an error. To avoid this, ensure that your directory name follows the naming conventions of the file system you are using. Stick to alphanumeric characters, underscores (_), and hyphens (-) to create valid directory names.
  • ๐Ÿ’พ Insufficient Disk Space: When creating a directory, the mkdir command requires free disk space to allocate the new directory. If your disk space is full or insufficient, you will encounter an error stating that there is not enough space to create the directory. To resolve this, you can free up disk space by deleting unnecessary files or move to a different location with sufficient available space. You can use disk usage analysis tools like df or du to check the disk space utilization and identify areas where you can free up storage.

In Conclusion

By following the step-by-step instructions outlined in this article, you can confidently use the mkdir command to create single directories, multiple directories, nested directories, directories with specific permissions, and creating directories with timestamp. However, it’s crucial to be aware of common errors that can occur when using the mkdir command.

If you’re eager to expand your understanding of Linux command line utilities, I recommend to explore related articles on advanced Linux commands, directory management, and shell scripting to expand your knowledge and expertise further. With these skills, you’ll be able to navigate the Linux environment with confidence and efficiency.

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 Text In Vim/Vi

Next Post

5 Best Methods to Install Deb Files on Ubuntu

Leave a Reply

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

Read next