How to Use the mkdir Command in Linux?


Mastering the mkdir Command in Linux: A Comprehensive Guide

Creating directories is one of the essential tasks in any operating system, and in Linux, the mkdir command is your go-to tool for this purpose. Whether you’re setting up a new project, organizing files, or managing a complex directory structure, mkdir provides the functionality you need to create directories efficiently.

Basic Usage

The simplest way to use mkdir is to specify the names of the directories you want to create. Here’s how:

mkdir path/to/directory1 path/to/directory2

This command will create directory1 and directory2 in the specified paths. If the paths do not exist, mkdir will return an error.

Creating Parent Directories

Sometimes, you need to create not just a single directory but a whole structure of directories in one go. The -p or --parents option allows you to do just that. This option creates any missing parent directories along the specified path:

mkdir -p path/to/directory1 path/to/directory2

In this example, if path/to does not exist, mkdir will automatically create it along with the specified directories.

Setting Directory Permissions

Another powerful feature of mkdir is the ability to set permissions for the directories you are creating. You can use the -m or --mode option to specify permissions in numeric (e.g., 755) or symbolic (e.g., rwxrw-r--) form:

mkdir -m rwxrw-r-- path/to/directory1 path/to/directory2

Here, directory1 and directory2 will be created with the specified permissions, allowing the owner to read, write, and execute; the group to read and write; and others to read only.

Summary

The mkdir command is a versatile tool in the Linux command line that allows you to create directories with various options tailored to your needs. Whether you’re simply creating a few folders or setting up an entire directory hierarchy with specific permissions, mkdir is equipped to handle it all with ease.

For more in-depth information about its various options and functionalities, check out the official documentation: GNU mkdir invocation.

Happy directory creating!

See Also