mkdir command in Linux /w examples

mkdir command in Linux

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.

Understanding the mkdir Command

Before we dive into examples, let’s grasp the basic syntax of the mkdir command:

mkdir [options] directory_name(s)

  • [options]: These are optional flags that modify the behavior of the mkdir command. 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:

mkdir my_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: dir1, dir2, and dir3.

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

The 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 mkdir command:

  • -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:

  1. 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.
  2. Use Descriptive Names: Choose descriptive names that reflect the contents or purpose of the directory. This makes it easier to organize and locate files.
  3. Plan Your Directory Structure: Think about the overall structure of your directories. Organize them logically to simplify file management.

Conclusion

The 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.

Tags: , ,



Top ↑