Linux mkdir Command

The mkdir command, an abbreviation for "make directory," is a fundamental utility in the Linux operating system used to create directories (folders). It's an essential tool for organizing and managing the file system, allowing users to structure their data efficiently. In this blog post, we will explore the various aspects of the mkdir command, including its syntax, options, and best practices.

Basic Syntax

The basic syntax of the mkdir command is straightforward:

bash
mkdir options directory_name
  • options: Additional flags that modify the behavior of the mkdir command.
  • directory_name: The name of the directory to be created.

Creating a Directory

Creating a new directory using mkdir is simple. You just provide the name of the directory you want to create as an argument:

bash
mkdir directory_name

Replace directory_name with the desired name for the directory.

Creating Multiple Directories

You can create multiple directories simultaneously by specifying multiple directory names as arguments:

bash
mkdir directory1 directory2 directory3

This will create three directories: directory1, directory2, and directory3.

Creating Nested Directories

The -p option, short for "parents," enables the creation of a nested directory structure. This is useful when you want to create a directory with subdirectories:

bash
mkdir -p parent_directory/child_directory

The -p option ensures that both the parent_directory and child_directory are created, even if parent_directory does not exist.

Setting Directory Permissions

The chmod command is often used in conjunction with mkdir to set permissions for the newly created directory. After creating a directory, you can modify its permissions using the following syntax:

bash
chmod permissions directory_name

For example, to grant read, write, and execute permissions to the owner and read and execute permissions to others, you would use:

bash
chmod 755 directory_name

Here, 7 represents full permissions for the owner (read, write, and execute), 5 represents read and execute permissions for the group and others.

Verifying Directory Creation

To confirm that a directory has been successfully created, you can use the ls (list) command. The -l option provides a detailed listing that includes information about the permissions, ownership, size, and modification time of the directory:

bash
ls -l