Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
Linux

How to Create a Directory in Linux with mkdir Command?

Introduction

Directories in Linux serve as containers for organizing and storing files and other directories. Much like folders in a filing cabinet, directories help users maintain a structured and orderly file system, making it easier to locate and manage files efficiently. The “mkdir” command, short for “make directory,” is a fundamental tool in Linux that allows users to create new directories quickly and easily. This article will delve into the functionality of the “mkdir” command in Linux, explaining its syntax and demonstrating how to create both single and multiple directories using this command. By understanding the basics of the mkdir command, users can effectively organize their file systems and streamline their workflow in Linux environments.

In Linux, What is the mkdir Command?

In Linux, What is the mkdir Command?

In Linux, the “mkdir” command stands for “make directory.” It’s like giving a command to create a new folder or directory on your computer. Just as you might create a new folder on your desktop to keep your files organized, the “mkdir” command does the same thing in the Linux operating system. When you use the “mkdir” command followed by the name you want to give to your new directory, Linux creates that directory for you. For example, if you type “mkdir documents” into the command line and press enter, Linux will create a new directory named “documents” in your current location. This command helps you organize your files and keep your computer tidy by creating new folders wherever you need them.

Linux mkdir Command Syntax 

Linux mkdir Command Syntax 

The syntax of the “mkdir” command in Linux refers to the specific structure and format that the command follows when used in the terminal. The basic syntax of the “mkdir” command is “mkdir [options] [directory_name].” Here’s what each part of the syntax means:

  • “mkdir”: This is the actual command that tells Linux to create a new directory.
Linux mkdir Command Syntax 
  • “[options]”: These are additional settings or parameters that you can include to modify the behavior of the “mkdir” command. Some common options include “-p” to create
  •  parent directories if they don’t exist and “-v” to display a message for each directory created.
Linux mkdir Command Syntax 
  • “[directory_name]”: This is the name of the directory you want to create. You can replace “[directory_name]” with the name you want to give to your new directory.

How to Create a New Directory In Linux?

  1. Open a terminal on your Linux system. You can usually find the terminal application in the system’s applications menu.

Once you have the terminal open, navigate to the location where you want to create the new directory. You can use the “cd” command to change directories. For example, if you want to create the new directory in your home directory, you can use the following command “cd ~

Now, use the “mkdir” command followed by the name you want to give to your new directory. For example, if you want to create a directory named “documents,” you can use the following command “mkdir documents.” This will create a new directory named “documents” in your current location.

Using mkdir How to Create Multiple Directories

To create multiple directories at once using the mkdir command in Linux, you can specify the names of the directories you want to create separated by spaces. Here’s how you can do it, use the command “mkdir directory1 directory2 directory3.”

Note – Replace “directory1”, “directory2”, and “directory3” with the names of the directories you want to create.

Using mkdir How to Create Multiple Directories

How to Create Parent Directories?

To create parent directories along with a new directory in Linux, you can use the “-p” option with the “mkdir” command. This option ensures that all parent directories leading up to the specified directory are created if they don’t already exist. Here’s how you can do it, use the following command: 

mkdir -p /path/to/parent/directory/newdirectory
How to Create Parent Directories?

Replace “/path/to/parent/directory/newdirectory” with the desired path for the new directory.

How to Set Permissions When Making a Directory?

How to Set Permissions When Making a Directory?

When creating a directory in Linux using the mkdir command, you can simultaneously set permissions for the directory using the chmod command.

Using Numeric Mode:

You can specify permissions using numeric mode with chmod. The numeric mode represents permissions for the user, group, and others, respectively.

For example, to create a directory with read, write, and execute permissions for the user, and read and execute permissions for the group and others, you can use:

mkdir new_directory
chmod 755 new_directory
How to Set Permissions When Making a Directory?

Combining mkdir and chmod:

You can combine the mkdir and chmod commands using the && operator to create a directory and set permissions in a single line.

mkdir new_directory && chmod 755 new_directory

Note – this is an example.

Also Read: Linux Commands Cheat Sheet: Advanced Techniques

Conclusion 

The “mkdir” command in Linux serves as a fundamental tool for creating directories or folders, facilitating the organization and management of files within the operating system. By understanding its syntax and functionality, users can efficiently create single or multiple directories, create parent directories, and even set permissions for directories during creation. Whether organizing personal files or managing system directories, the “mkdir” command empowers users to maintain a structured and orderly file system in Linux environments. By leveraging the flexibility and versatility of the “mkdir” command, users can enhance their productivity and streamline their workflow in Linux.

Arpit Saini

He is the Director of Cloud Operations at Serverwala and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles