Table of contents
1.
Introduction 
2.
mkdir Command in Linux
3.
To Make Multiple Directories
4.
Mkdir Options
4.1.
-m or --mode
4.2.
-v or --verbose
4.3.
-p or --parents
5.
Frequently Asked Questions 
5.1.
Can mkdir create nested directories?
5.2.
What happens if I try to create a directory that already exists?
5.3.
How do I set permissions while creating a directory?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

MKDIR Command in Linux

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Navigating the digital landscape requires a solid grasp of various commands, especially when you're a student immersed in the world of coding and Linux. Among these, the mkdir command in Linux stands out as a fundamental tool for managing directories. This article aims to unravel the intricacies of the mkdir command. 

MKDIR Command in Linux

You'll learn about its basic definition, how to create multiple directories, and explore various options associated with it. By the end of this guide, you'll be equipped with practical knowledge and skills to efficiently use mkdir in your coding endeavors.

mkdir Command in Linux

The mkdir command in Linux is a staple for anyone working in a Unix-based environment. It stands for "make directory" and is used to create new directories in the file system. Whether you're organizing files for a project, structuring your work environment, or setting up directories for applications, mkdir is your go-to command.

To Make Multiple Directories

Creating a single directory is just the beginning. Often, you'll find the need to create multiple directories at once, perhaps to set up a complex project structure. With mkdir, you can achieve this efficiently. Let's dive into an example to see how it's done.

Suppose you're working on a project with a standard structure: a main project folder containing subfolders for src (source files), docs (documentation), and tests (test files). Here's how you'd set it up in one go:

mkdir -p MyProject/{src,docs,tests}


The -p option tells mkdir to create the parent directory (MyProject) if it doesn't exist and then create the src, docs, and tests directories inside it.

Mkdir Options

mkdir offers various options to tailor its functionality. Here are some key ones:

-m or --mode

 Set file permissions while creating the directory. For instance, mkdir -m 755 NewDir creates a directory with read, write, and execute permissions for the owner, and read and execute permissions for others.

-v or --verbose

Provides feedback by displaying a message for each directory created. Useful for tracking what's being created, especially in scripts.

-p or --parents

As demonstrated earlier, this option creates parent directories as needed.

Each of these options can be combined for more complex operations. For instance, creating multiple directories with specific permissions would look like this:

mkdir -pv -m 755 Project/{assets,logs,config}


This command creates a Project folder with assets, logs, and config subdirectories, all with 755 permissions, and provides verbose output.

Frequently Asked Questions 

Can mkdir create nested directories?

Yes, using the -p option, mkdir can create nested directories in a single command.

What happens if I try to create a directory that already exists?

Without the -p option, mkdir will return an error if the directory already exists.

How do I set permissions while creating a directory?

Use the -m option followed by the permission value, like mkdir -m 755 MyDir.

Conclusion

Mastering the mkdir command in Linux is a fundamental skill for any coding student. Its versatility in creating single or multiple directories, coupled with options for permissions and verbose output, makes it an essential tool in your Linux toolkit. Understanding and applying these concepts will streamline your directory management and enhance your overall efficiency in Linux environments.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass