The mkdir command, short for “make directory,” is a fundamental tool in the Linux command line arsenal. It enables users to create directories or folders effortlessly. In this article, we’ll explore the essentials of using the mkdir command in Linux, complete with practical examples to help you become proficient in directory creation.
This article is a follow-up to the previous 90 Linux Commands frequently used by Linux Sysadmins post. As time allows, I will continue to publish articles on each of these 90 commands geared toward Linux sysadmins and Linux power users.
Table of Contents
Before we dive into examples, let’s grasp the basic syntax of the
mkdir [options] directory_name(s)
[options]: These are optional flags that modify the behavior of the
mkdircommand. We’ll cover some common options in the examples.
directory_name(s): These are the names of the directories you want to create.
Creating a Single Directory with mkdir
The primary function of the
mkdir command is to create directories. Here’s how to create a single directory:
This command creates a directory named
my_directory in the current location.
Creating Multiple Directories with mkdir
You can create multiple directories in one go by specifying their names:
mkdir dir1 dir2 dir3
This command creates three directories:
Creating Parent Directories with mkdir
If you want to create a directory and its parent directories (if they don’t exist), you can use the
-p (parents) option:
mkdir -p parent/child/grandchild
This command creates the directory structure
parent/child/grandchild, ensuring that all parent directories exist.
Setting Permissions with mkdir
mkdir command also allows you to set permissions for the newly created directory using the
-m (mode) option:
mkdir -m 755 my_directory
This command creates
my_directory with the specified permission mode (in this case,
755, which provides read, write, and execute permissions to the owner and read and execute permissions to others).
mkdir Common Options
Here are some common options used with the
-p(parents): Creates parent directories if they don’t exist, as mentioned earlier.
-m(mode): Sets the permissions mode for the directory, as demonstrated above.
-v(verbose): Displays detailed information about the directories created.
Directory Naming and Best Practices
When creating directories in Linux, it’s essential to follow naming conventions and best practices:
- Avoid Spaces and Special Characters: Use lowercase letters and underscores or hyphens to separate words in directory names. Avoid spaces and special characters to ensure compatibility.
- Use Descriptive Names: Choose descriptive names that reflect the contents or purpose of the directory. This makes it easier to organize and locate files.
- Plan Your Directory Structure: Think about the overall structure of your directories. Organize them logically to simplify file management.
mkdir command is a versatile tool for creating directories in Linux. Whether you need to create a single directory, multiple directories, or an entire directory structure, it offers flexibility and efficiency. By mastering the
mkdir command, you can effectively manage Linux file organization and directory hierarchy.
Now that you’ve learned the essentials of the mkdir command, you can confidently create directories in your Linux environment, following best practices for naming and organization.
If you’re interested in expanding your knowledge of Linux commands and want to explore more frequently used commands by Linux sysadmins, I recommend checking out the parent article at the following link: 90 Linux Commands Frequently Used by Linux Sysadmins.