Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Linux 'mkdir' command

April 23, 2024

Mastering the mkdir Command in Linux

As a programmer, it is essential to develop familiarity with the command-line interface in Linux. One fundamental command that every Linux user should master is mkdir. This command allows you to create directories and organize your files efficiently. In this blog post, we will explore the various functionalities of the mkdir command and provide you with extensive examples.

Creating a Single Directory

The basic usage of the mkdir command is to create a single directory. To create a directory named “mydir”, you can simply run the following command:

1
mkdir mydir

In this case, the mkdir command creates a new directory named “mydir” in the current working directory.

Creating Nested Directories

Sometimes, you may need to create directories within directories, forming a nested structure. To achieve this, simply specify the parent directories separated by slashes (/). For instance:

1
mkdir -p parent/child/grandchild

The -p flag tells mkdir to create the parent directories as needed. In the example above, we create a nested structure with a parent directory “parent,” a child directory “child,” and a grandchild directory “grandchild.”

Creating Multiple Directories at Once

The mkdir command also enables you to create multiple directories simultaneously by providing space-separated directory names as arguments. Do note that if any of the directories already exist, an error will be raised. For instance:

1
mkdir dir1 dir2 dir3

In this example, the mkdir command creates three directories named “dir1,” “dir2,” and “dir3” within the current working directory.

Creating Directories with Specific Permissions

By default, when you create a directory using mkdir, it inherits the permissions of its parent directory. However, you can override the default permissions by specifying the appropriate flags.

To change the permissions of a directory during creation, you can use the chmod flag followed by the permissions you desire, represented in numeric format or symbolic notation.

For numeric format:

1
mkdir -m 755 mydir

In the above example, we create a directory “mydir” with permissions set to “755.” This provides read, write, and execute permissions to the owner of the directory, while granting only read and execute permissions to others.

Using symbolic notation, you can achieve the same result:

1
mkdir -m u=rwx,g=rx,o= mydir

This command creates “mydir” with user, group, and others' permissions explicitly specified.

Summary

The mkdir command is a powerful tool for creating directories efficiently in Linux. With just a few options and flags, you can create single directories, nested structures, multiple directories simultaneously, and even set specific permissions. The command-line interface provides flexibility and control, allowing you to manage your file organization effortlessly.

These examples should give you a solid foundation for using the mkdir command effectively in your day-to-day programming tasks.


➡️ Deep Dive into ESP32 WiFi Connectivity: Understanding WiFi Modes and Configurations


⬅️ Exploring the GPIOs in ESP32: Usage and Configuration


Go back to Posts.