Have you ever wanted to make a directory in Linux, but didn’t know how? The `mkdir` command is the answer! mkdir stands for “make directory”, and it’s one of the most useful Linux commands. It allows you to create directories (folders) in the Linux file system. You can create a single directory with a single command, or multiple directories at once with a single command. It’s incredibly useful for organizing files, and it can save you a lot of time and hassle.

Advertisement

In this article, we will go over 10 practical examples of the mkdir command in Linux.

Syntax

The mkdir command uses the following syntax:

mkdir [OPTION]... DIRECTORY...

mkdir Command Examples

Here are a few command line examples for creating directories with mkdir command on a Unix/Linux system.

  1. Create a single directory

    The following command is to create a directory in the present working directory. Just use the mkdir command followed by the new directory name to be created.

    mkdir my_dir 
    
  2. Create directory and its parent

    When creating a directory at another location, the parent directories must be available. If the parent directories are not available it will through an error with the above examples.

    But the mkdir command provides an option to also create parent directories.

    mkdir -p /opt/backup/my_dir 
    

    Here the -p tells mkdir to also create parent directories well.

  3. Create multiple directories

    You can also create multiple directories in a single command.

    mkdir dir1 dir2 dir3 
    

    You can also ass all the directory names including paths to create multiple directories in a single command.

    mkdir dir1 /backup/dir2 /var/www/dir3 
    
  4. Create a directory with specific permissions

    Use -m option to set default permission to a directory during creation time. This is similar to creating a directory and changing permission with `chmod` command.

    mkdir -m 644 my_dir  
    mkdir -m a=rwx my_dir 
    mkdir -m u=rw,g=r,o=r my_dir 
    
  5. Create a directory with specific owner and group

    We can define the directory owner and group at the time of creation, For example:

    mkdir -m 755 -p -v --owner=user --group=group my_dir 
    

    Here --owner is used to define the user and --group is to define group name.

  6. Create multiple directories at specific location

    This is the lesser-known option for creating multiple directories at different locations with a single command. The following command will create 3 directories (one, two, and three) under the `/var/backup` directory.

    mkdir -p /var/backup/{dir1|dir2|dir3} 
    
  7. Create a directory and set the SELinux context

    The RHEL-based systems use SELinux for the enhanced security of files. You can also define the SELinux context during directory creation using the -Z option:

    mkdir -Z context_type my_dir 
    

  • Conclusion

    In conclusion, the mkdir command is a simple and powerful tool for creating directories in Linux. By using the various options and arguments, you can create directories with custom permissions, owners, and SELinux context types, and you can even create multiple directories in one command. Becoming familiar with the mkdir command is an important step in becoming proficient in Linux administration.

  • Share.
    Leave A Reply


    Exit mobile version