The mkdir command is a fundamental utility in RHEL and other Linux distributions used to create new directories or folders. Properly organizing files into directories is a cornerstone of efficient system administration and user file management.
Example 1: Create a single new directory
# Create a directory named 'reports' in the current location
mkdir reports
Explanation: This command creates a new directory named reports. If you are in your home directory (/home/user), this command will create /home/user/reports.
Example 2: Create multiple directories at once
# Create three separate directories: 'invoices', 'quotes', and 'archive'
mkdir invoices quotes archive
Explanation: By listing the names separated by spaces, mkdir can create several directories in the same location simultaneously. This is more efficient than typing the command three separate times.
Example 3: Create a directory with a specific path
# Create a directory named '2025' inside the '/var/log' directory
mkdir /var/log/2025
Explanation: This example demonstrates creating a directory in a location other than the current one by providing an absolute path. You must have the necessary write permissions in /var/log to execute this.
Example 4: Create a nested directory structure (parent directories)
# Create the 'invoices' directory inside '2025', inside 'sales', creating parent directories as needed
mkdir -p sales/2025/invoices
Explanation: The -p (parent) option is a powerful feature that instructs mkdir to create any necessary parent directories that do not already exist. Without -p, the command would fail if the sales or 2025 directories were not already present.
Example 5: Create a directory with specific permissions
# Create a directory named 'shared' with read, write, and execute permissions for all users
mkdir -m 777 shared
Explanation: The -m (mode) option allows you to set the directory's file permissions at the time of creation. In this case, 777 grants read, write, and execute permissions to the owner, the group, and all other users.
Example 6: Create a directory with verbose output
# Create the 'project_alpha' directory and print a confirmation message
mkdir -v project_alpha
Explanation: The -v (verbose) option provides feedback, confirming that the directory was successfully created. This is useful in scripts or when you want explicit confirmation of the command's execution.
Example 7: Creating a directory with a space in its name
# Create a single directory named 'My Documents'
mkdir "My Documents"
Explanation: To create a directory with a space or other special characters in the name, you must enclose the name in quotation marks. This ensures that the shell interprets "My Documents" as a single argument.