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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.