How to use the command 'mkdir' (with examples)

How to use the command 'mkdir' (with examples)

The mkdir command is a fundamental operation available in Unix-like operating systems, used primarily for creating directories. It offers several options to enhance its usability, including setting permissions and creating parent directories automatically. The mkdir command is essential for organizing files and directories in a structured manner within a file system.

Use case 1: Create specific directories

Code:

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

Motivation:

Creating directories is a primary task when organizing your file system. Whether you’re setting up a new project or simply need folders to categorize documents, the mkdir command provides a straightforward way to build multiple directories at once. By specifying multiple directory paths, you save time and effort instead of creating each one individually.

Explanation:

  • mkdir: This is the command used to create directories. It tells the system that you want to make one or more new directories.
  • path/to/directory1 path/to/directory2 ...: These are the paths where you want your new directories to be created. You can create multiple directories at various paths in just one command by listing them with a space in between.

Example output:

Assuming the command mkdir projects/docs projects/images is executed, you won’t see any output if the operation runs successfully. However, two new directories named docs and images will appear inside the projects directory.

Use case 2: Create specific directories and their parents if needed

Code:

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

Motivation:

When building nested directory structures, there may be parent directories that do not yet exist. The -p or --parents option simplifies the process by creating all necessary parent directories leading up to the final directory in the path. This is especially useful in scripts or when setting up complex, multi-level folder structures, as it prevents errors and saves time.

Explanation:

  • mkdir: Again, this is the command to create directories.
  • -p or --parents: This option tells mkdir to create any missing parent directories specified in the path. Without this option, mkdir would return an error if any of the parent directories do not exist.
  • path/to/directory1 path/to/directory2 ...: These specify the full paths of directories you want to create. mkdir will ensure every level of the path exists by creating directories along the way.

Example output:

Running mkdir -p projects/reports/2023/january means that even if the directories projects, reports, 2023, and january don’t exist, they will all be created in one go without errors.

Use case 3: Create directories with specific permissions

Code:

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

Motivation:

Security and access permissions are critical when managing files and directories. The -m or --mode option allows you to set directory permissions at creation time, ensuring that only the right users can access or modify the contents. This feature is invaluable in multi-user environments or when directories contain sensitive information that needs controlled permissions from the get-go.

Explanation:

  • mkdir: The command to create directories.
  • -m or --mode: This option allows you to set the file mode (permissions) at the time of directory creation.
  • rwxrw-r--: The permission settings for the directories. In this example, the owner has read, write, and execute permissions, the group has read and write permissions, and others have read permissions only. These settings are specified using standard Unix permission notation.
  • path/to/directory1 path/to/directory2 ...: Locations where new directories will be created with the specified permissions.

Example output:

After executing mkdir -m 754 secured/docs secured/images, directories docs and images will be created with the specified permissions where the owner can read, write, and execute, group members can read and write, and others can only read.

Conclusion:

The mkdir command proves itself as a versatile tool for managing directories within a Unix-like filesystem. By leveraging the ability to create multiple directories, automatically build parent directories, and set permissions, users can organize their file structures efficiently and securely. Whether for personal projects or professional environments, mastering the nuances of mkdir optimizes file management tasks and enhances system organization.

Related Posts

How to use the command 'dhcpwn' (with examples)

How to use the command 'dhcpwn' (with examples)

The dhcpwn is a command-line utility designed for testing DHCP IP exhaustion attacks and sniffing local DHCP traffic.

Read More
Managing Your OpenShift Environment with the 'oc' Command (with examples)

Managing Your OpenShift Environment with the 'oc' Command (with examples)

The OpenShift Container Platform Command Line Interface, known simply as oc, is a powerful tool for developers and administrators alike.

Read More
How to Use the 'eget' Command (with Examples)

How to Use the 'eget' Command (with Examples)

The eget command is a versatile tool designed for easily installing prebuilt binaries from GitHub.

Read More