mkdir: Making a New Directory in Linux

Introduction

In the world of Linux, working with directories (folders) is a fundamental aspect of managing files and organizing data. One of the most essential commands for directory management is ‘mkdir,’ which stands for “make directory.” This article aims to provide a comprehensive guide on how to use the ‘mkdir’ command effectively and efficiently in Linux.

Understanding the ‘mkdir’ Command

The mkdir command in Linux is a basic and essential tool for managing files and directories. As a user of a Linux system, it is important to understand the mkdir command, how to use it, and the various options available. This article aims to provide a comprehensive guide to the mkdir command and its uses in Linux.

What is the mkdir Command in Linux?

The mkdir command is used to create a new directory in Linux. The name “mkdir” stands for “make directory.” With the mkdir command, you can create a new directory, or multiple directories at once, in your file system. This command is available in all major Linux distributions, including Debian, Ubuntu, Fedora, and CentOS.



Syntax of the mkdir Command

The basic syntax of the mkdir command is simple. To create a new directory using the mkdir command, simply type the following command in your terminal:

mkdir directory_name

Where directory_name is the name of the directory you want to create.



Options of the mkdir Command

The mkdir command has several options available that allow you to customize the behavior of the command. Some of the most commonly used options include:

-p (–parents)

The -p or –parents option allows you to create a directory along with its parent directories if they do not exist. For example, if you want to create a directory /home/user/new_dir but /home/user does not exist, the -p option will create both the /home/user and /home/user/new_dir directories.


-m (–mode)

The -m or –mode option allows you to specify the permissions for the new directory. For example, if you want to create a directory with read and execute permissions for all users, you would use the following command:

mkdir -m 755 directory_name

-v (–verbose)

The -v or –verbose option displays verbose output, which can be useful for debugging or understanding what the command is doing. For example, when using the -v option, the following command would display information about the new directory as it is being created:

mkdir -v directory_name


Examples of the mkdir Command in Action

Now that we have a basic understanding of the mkdir command and its options, let’s look at some examples of how to use the command in practice.



Creating a New Directory

To create a new directory, simply run the following command:

mkdir new_dir

This command will create a new directory named new_dir in your current working directory.



Creating Multiple Directories

To create multiple directories at once, simply specify multiple directory names in the same command. For example:

mkdir dir1 dir2 dir3

This command will create three new directories, dir1, dir2, and dir3, in your current working directory.



Creating Directories with Parent Directories

To create a directory along with its parent directories, use the -p option. For example:

mkdir -p /home/user/new

FAQs

Q: Can I create multiple directories at once using “mkdir” in Linux?

Yes, you can create multiple directories simultaneously using the “mkdir” command. To achieve this, simply provide the names of all the directories you want to create as arguments to the “mkdir” command. For example:

mkdir dir1 dir2 dir3

Q: How do I create a parent directory and its subdirectories together?

To create a parent directory along with its subdirectories in one command, you can use the “-p” option with “mkdir.” This option enables recursive directory creation, meaning it creates parent directories if they don’t exist. For instance:

mkdir -p parent_dir/sub_dir/sub_sub_dir

Q: What if I want to create a directory with a specific set of permissions?

If you wish to create a directory with specific permissions, you can combine the “mkdir” command with the “chmod” command. The “chmod” command is used to change file permissions in Linux. For example:

mkdir my_directory
chmod 755 my_directory