thelinuxvault blog

How to Create Directory in Linux | mkdir Command

In the Linux operating system, the ability to manage files and directories effectively is fundamental. One of the most basic yet essential operations is creating directories. Directories, also known as folders, provide a structured way to organize files and other sub - directories. In Linux, the mkdir command is the go - to tool for creating new directories. This blog post will guide you through the ins and outs of the mkdir command, including basic usage, advanced options, common practices, and best practices.

2026-06

Table of Contents#

  1. Basic Syntax of mkdir
  2. Creating a Single Directory
  3. Creating Multiple Directories at Once
  4. Creating Parent Directories Recursively
  5. Setting Directory Permissions While Creating
  6. Common Practices
  7. Best Practices
  8. Troubleshooting
  9. Conclusion
  10. References

1. Basic Syntax of mkdir#

The basic syntax of the mkdir command is straightforward:

mkdir [options] directory_name

Here, options are the command - line flags that modify the behavior of the mkdir command, and directory_name is the name of the directory you want to create.

2. Creating a Single Directory#

To create a single directory, you simply need to provide the name of the directory after the mkdir command. For example, to create a directory named new_folder, you would run the following command:

mkdir new_folder

You can verify that the directory has been created by using the ls command:

ls

This will list all the files and directories in the current working directory, and you should see new_folder in the list.

3. Creating Multiple Directories at Once#

If you need to create multiple directories at once, you can list all the directory names separated by a space. For example:

mkdir folder1 folder2 folder3

Running the ls command after this will show that folder1, folder2, and folder3 have been created in the current working directory.

4. Creating Parent Directories Recursively#

Sometimes, you may want to create a directory inside another directory that doesn't exist yet. By default, the mkdir command will give an error in such cases. However, you can use the -p or --parents option to create the parent directories recursively.

For example, let's say you want to create a directory structure like parent_dir/child_dir. If parent_dir doesn't exist, you can use the following command:

mkdir -p parent_dir/child_dir

This command will first create parent_dir if it doesn't exist, and then create child_dir inside it.

5. Setting Directory Permissions While Creating#

You can use the -m or --mode option to set the permissions of the directory while creating it. The permissions are specified in octal notation. For example, to create a directory named secure_folder with read, write, and execute permissions only for the owner, you can use the following command:

mkdir -m 700 secure_folder

The 700 octal value represents rwx (read, write, execute) for the owner and no permissions for the group and others.

6. Common Practices#

  • Use Descriptive Names: When creating directories, use names that clearly describe their purpose. For example, if you are creating a directory to store all your project - related files, name it something like project_name_files.
  • Organize Hierarchically: Create a hierarchical directory structure to make it easier to manage files. For example, you can have a top - level directory for all your work projects, and inside it, create separate directories for each project.

7. Best Practices#

  • Check for Existing Directories: Before creating a directory, it's a good practice to check if it already exists. You can use the test -d command to check if a directory exists. For example:
if [ ! -d "new_dir" ]; then
    mkdir new_dir
fi
  • Use Environment Variables: If you are creating directories in a script, use environment variables to make the script more flexible. For example, you can use the $HOME environment variable to create a directory in the user's home directory:
mkdir $HOME/my_project_folder

8. Troubleshooting#

  • Permission Denied: If you get a "Permission denied" error when trying to create a directory, it means you don't have the necessary permissions to create a directory in the specified location. You can try running the command with sudo to gain administrative privileges. For example:
sudo mkdir /var/www/new_site
  • Directory Already Exists: If you try to create a directory that already exists, the mkdir command will give an error. You can use the -p option to avoid this error, as it will not raise an error if the directory already exists.

9. Conclusion#

The mkdir command in Linux is a simple yet powerful tool for creating directories. By understanding its basic syntax, advanced options, and following common and best practices, you can efficiently manage your directory structure in Linux. Whether you are a beginner or an experienced Linux user, mastering the mkdir command is essential for effective file and directory management.

10. References#