linux mkdir

Create New Directory in Linux/Unix by mkdir

Before going into details, this command will create a new directory (or folder) in the Linux system:

mkdir test_dir

A little intro about Linux/Unix mkdir command

The mkdir command in Linux/Unix is a tool used for creating new directories. It stands for “make directory” and allows users to create directories with specific names and permissions. Here’s an overview of the mkdir command

So, let us start with a simple mkdir command

The general syntax of the mkdir is:

$ mkdir [option] dir_name

For example:

$ mkdir new_dir
How to create multiple directories with a single command?

For creating multiple directories with a single command, just separate the directory names with space i.e.

mkdir <directory1> < directory 2> < directory3>

You may also use curly braces for creating multiple directories:

mkdir {directory1, directory2, directory3}
Let us dive into mkdir options

As shown in the syntax, the mkdir command has a few options that you may use easily. A few are explained below with example commands.

The –p option in mkdir

The –p options is used to create parent directories. So, if you want to create a directory tree, this is possible.

An example of –p is:

mkdir -p parent1/child1
The –m option for permissions

By default, the mkdir generates a directory with permissions set by umask. To induce mkdir to make a directory with various permissions the -m option may be utilized. For instance to make directory [[chmod | permissions]] 744 we may use following Linux command:

mkdir -m 744 directory_perm

Full read, write, and execute permissions are given for all users by the above command.

The –v option

The –v or –verbose is used to print the message for every new directory created. For example:

mkdir -v [directories]

An error message is displayed if the directory already exists.

FAQs about Making New Directories in Linux

Q1: What is the basic command to create a new directory in Linux/Unix using mkdir?

Further Reading:  Linux Find: How to Search Files with Example Commands

A1: The basic command is mkdir directory_name. For example: mkdir test_dir.

Q2: What does mkdir stand for in Linux/Unix?

A2: mkdir stands for “make directory.” It is a command used to create new directories.

Q3: Can I set permissions while creating a new directory with mkdir?

A3: Yes, you can set permissions using the -m option.

Example:

mkdir -m 744 directory_perm

Q4: Is it possible to create multiple directories with a single mkdir command?

A4: Yes, you can create multiple directories by separating their names with spaces or using curly braces.

Example:

mkdir dir1 dir2 dir3 or mkdir {dir1, dir2, dir3}

Q5: What is the purpose of the -p option in mkdir?

A5: The -p option is used to create parent directories. It allows the creation of a directory tree.

Example:

mkdir -p parent1/child1

Q6: How can I see messages for every new directory created with mkdir?

A6: Use the -v or --verbose option.

Example:

mkdir -v dir1 dir2 dir3

Q7: What happens if I try to create a directory that already exists?

A7: If the directory already exists, mkdir will display an error message.

Q8: Can I create directories with specific permissions using mkdir?

A8: Yes, you can use the -m option to specify permissions.

Example:

mkdir -m 744 directory_perm

Q9: Do I need special permissions to create a directory with mkdir?

A9: The user executing the command should have sufficient permissions in the parent directory; otherwise, a ‘permission denied‘ error may occur.

Q10: Is there a way to create multiple directories in a directory tree with mkdir?

A10: Yes, you can use the -p option to create parent directories and establish a directory tree.

Example:

mkdir -p parent1/child1

Best Practices for Creating Directories in Linux/Unix with mkdir

  1. Understand the Basic Command:
    • Begin with the simple command mkdir directory_name to create a new directory. For example: mkdir test_dir.
  2. Comprehend the Purpose of mkdir:
    • Understand that mkdir stands for “make directory” and is designed for creating new directories in the Linux/Unix system.
  3. Consider Permissions:
    • Be aware that permissions can be set while creating directories using the -m option. Example: mkdir -m 744 directory_perm.
  4. Use mkdir for Multiple Directories:
    • When creating multiple directories, separate their names with spaces or use curly braces. Example: mkdir dir1 dir2 dir3 or mkdir {dir1, dir2, dir3}.
  5. Be Mindful of User Permissions:
    • Ensure that the user executing the mkdir command has sufficient permissions in the parent directory. A lack of permissions may result in a ‘permission denied’ error.
  6. Explore Options for Directory Creation:
    • Familiarize yourself with the options available with the mkdir command, such as -p for creating parent directories. Example: mkdir -p parent1/child1.
  7. Set Specific Permissions with -m Option:
    • Use the -m option to set specific permissions for the newly created directory. This can be helpful for controlling access to the directory.
  8. Print Messages for Visibility:
    • Utilize the -v or --verbose option to print a message for every new directory created. This enhances visibility during the directory creation process.
  9. Handle Existing Directories:
    • Be aware that an error message is displayed if you attempt to create a directory that already exists. Consider checking for existing directories before running the command.
  10. Consider Directory Tree Creation:
  • If you need to create a directory tree, use the -p option to create parent directories as needed. Example: mkdir -p parent1/child1.
  1. Document Permissions Clearly:
    • Clearly document the permissions set using the -m option, especially when creating directories with specific access requirements.
Further Reading:  Why using zip in Linux?

By adhering to these best practices, you can effectively and securely create directories in a Linux/Unix environment using the mkdir command.

Wrapping up

In conclusion, the mkdir command in Linux/Unix serves as a versatile tool for efficiently creating directories. As a fundamental command, it provides users with the ability to make directories, set permissions, and facilitate the creation of multiple directories simultaneously. Understanding the basics of mkdir is essential, as it empowers users to organize their file systems with precision.

Key considerations include being mindful of user permissions, especially when executing the command in parent directories. Ensuring that the user has sufficient permissions prevents ‘permission denied’ errors. Additionally, exploring options such as -m for setting permissions and -p for creating parent directories expands the functionality of mkdir.

Best practices involve documenting permissions clearly, using the -v option for enhanced visibility, and handling existing directories with care. Whether creating a single directory or a directory tree, the mkdir command provides flexibility and control over the directory creation process.

Further Reading:  A Little About Vim and How to Search in it

By incorporating these best practices, users can navigate the mkdir command effectively, ensuring the secure and efficient establishment of directories in their Linux/Unix systems.