Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

mkdir Command in Linux with Examples

Last Updated on November 23, 2023 by Abhishek Sharma


Creating directories is a fundamental operation in Linux systems, essential for organizing files, programs, and data. The mkdir command, short for "make directory," is a powerful utility that allows users to create directories effortlessly from the command line interface. Understanding how to use mkdir effectively can streamline file management tasks and simplify directory structuring in Linux environments.

In this article, we’ll delve into the mkdir command, exploring its syntax, various options, and practical examples to illustrate its usage in different scenarios. By the end of this guide, you’ll have a comprehensive understanding of how to leverage mkdir to create directories efficiently in Linux.

What is the mkdir command in linux?

The mkdir command in Linux is used to create directories or folders within a file system. It stands for "make directory" and allows users to create one or more directories at a specified path.

Here are some examples demonstrating the usage of the mkdir command:

Creating a Single Directory:
To create a single directory, simply specify the directory name:

mkdir new_directory

This command will create a directory named "new_directory" in the current working directory.

Creating Multiple Directories:
You can create multiple directories simultaneously by providing their names separated by spaces:

mkdir dir1 dir2 dir3

This command will create three directories named "dir1," "dir2," and "dir3" in the current working directory.

Creating a Nested Directory Structure:
The -p option allows you to create a nested directory structure. If any directories in the path don’t exist, they will be created along with the final directory:

mkdir -p path/to/nested/directory

This command will create a directory structure where path/to/nested/directory will be created, along with any intermediate directories if they don’t exist.

Setting Permissions on a New Directory:
You can set permissions for the newly created directory using the -m option followed by the desired permissions:

mkdir -m 755 new_directory

This command will create a directory named "new_directory" with permissions rwxr-xr-x (read, write, and execute for the owner, and read/execute for group and others).

Creating Directories with Spaces in the Name:
To create a directory with spaces in its name, enclose the directory name within quotes:

mkdir "directory with spaces”

This will create a directory named "directory with spaces."

Suppressing Error Messages for Existing Directories:
Using the -p option suppresses error messages if the directory already exists. It allows you to create new directories without displaying errors for existing ones:

mkdir -p existing_directory

This command will not prompt an error message if "existing_directory" already exists and will proceed to create any new directories specified.

Conclusion:
The mkdir command is a versatile tool in the Linux command line toolkit, enabling users to create directories swiftly and efficiently. Its flexibility in creating single or multiple directories, handling nested structures, setting permissions, and suppressing error messages makes it an indispensable utility for managing file systems.

By mastering the various options and syntax of mkdir, users can streamline their workflow, organize their file structures, and enhance their overall productivity in Linux environments. Whether it’s creating directories with specific permissions, nested structures, or unique names, mkdir provides the necessary functionality to simplify directory creation tasks for users at any level of expertise in Linux.

Through this guide, you’ve gained insights into the mkdir command, allowing you to harness its capabilities and optimize your directory creation process in Linux. Experimenting with different options and examples provided here will further solidify your understanding and proficiency with mkdir.

FAQ (Frequently Asked Questions) about the mkdir command

Here are some FAQs related to mkdir Command:

1. What is the basic syntax of the mkdir command?
The basic syntax for mkdir is: mkdir [options] directory_name(s). It creates one or more directories with the specified name(s).

2. How can I create a single directory using mkdir?
To create a single directory, simply type mkdir directory_name.

3. How do I create multiple directories at once with mkdir?
To create multiple directories simultaneously, use the command mkdir directory1 directory2 directory3. You can add as many directory names as needed, separated by spaces.

4. Can mkdir create nested directories?
Yes, mkdir can create nested directories using the -p (parents) option. For instance, mkdir -p path/to/new/directory will create a directory structure with nested subdirectories if they don’t already exist.

5. Is it possible to set permissions while creating directories with mkdir?
Yes, you can set permissions using the -m option followed by the desired permissions. For example, mkdir -m 755 new_directory will create a directory with permissions set to rwxr-xr-x for the owner, and r-xr-xr-x for group and others.

6. How can I create directories with spaces in their names?
To create directories with spaces, enclose the directory name within quotes. For instance, mkdir "directory with spaces" will create a directory named "directory with spaces".

7. Is there a way to suppress error messages when creating directories using mkdir?
Yes, the -p option can be used to suppress error messages when trying to create directories that already exist. It allows you to create new directories without displaying errors for existing ones.

Leave a Reply

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