How to use the command mkdir (with examples)

How to use the command mkdir (with examples)

The mkdir command is used to create directories in a filesystem. It is a basic command that allows users to create directories with different options such as setting permissions or creating parent directories if needed.

Use case 1: Create specific directories

Code:

mkdir path/to/directory1 path/to/directory2 ...

Motivation: This use case is useful when you want to create specific directories in a filesystem. For example, if you want to create directories for each month of the year, you can use this command to quickly create them.

Explanation:

  • mkdir: The command itself to create directories.
  • path/to/directory1 path/to/directory2 …: The paths of the directories you want to create. You can specify multiple paths separated by spaces.

Example output: If you run the command mkdir month1 month2 month3, three directories will be created: month1, month2, and month3.

Use case 2: Create specific directories and their [p]arents if needed

Code:

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

Motivation: This use case is useful when you want to create directories and their parent directories if they don’t already exist. It saves you from having to manually create parent directories before creating the desired directory.

Explanation:

  • mkdir: The command itself to create directories.
  • -p: This option tells mkdir to create parent directories if they don’t exist.
  • path/to/directory1 path/to/directory2 …: The paths of the directories you want to create. You can specify multiple paths separated by spaces.

Example output: If you run the command mkdir -p path/to/parent/directory/subdirectory, it will create the directories path/to/parent/directory and path/to/parent/directory/subdirectory. If the parent directories exist, they are not affected.

Use case 3: Create directories with specific permissions

Code:

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

Motivation: This use case is useful when you want to create directories with specific permissions. Permissions determine who can access or modify the files and directories in a filesystem. Setting permissions at the time of creation saves you from having to manually change them afterwards.

Explanation:

  • mkdir: The command itself to create directories.
  • -m: This option is used to set permissions for the directories being created.
  • rwxrw-r–: The permission string that represents the desired permissions. In this example, it allows the owner to read (r), write (w), and execute (x) the directories, group members to read (r) and write (w), and others to only read (r).
  • path/to/directory1 path/to/directory2 …: The paths of the directories you want to create. You can specify multiple paths separated by spaces.

Example output: If you run the command mkdir -m rwxrw-r-- directory1 directory2, it will create two directories with the specified permissions. Running ls -l will show the directories with their assigned permissions.

Related Posts

Using Pyrit for WPA/WPA2 Wi-Fi Security Cracking (with examples)

Using Pyrit for WPA/WPA2 Wi-Fi Security Cracking (with examples)

1: Display system cracking speed The Pyrit command pyrit benchmark is used to display the system’s cracking speed.

Read More
How to use the command mate-calc (with examples)

How to use the command mate-calc (with examples)

The command mate-calc is used to start the calculator in the MATE desktop environment.

Read More
Keeping Rust Updated (with examples)

Keeping Rust Updated (with examples)

Introduction Rust provides a command-line tool called rustup that is used for managing Rust toolchains and associated packages.

Read More