Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»mkdir Command in Unix/Linux with Examples

    mkdir Command in Unix/Linux with Examples

    By RahulFebruary 5, 20233 Mins Read

    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.

    • Linux cd Command Help and Examples

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

    3. Create directory and its parent
    4. 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.

    5. Create multiple directories
    6. 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 
      

    7. Create a directory with specific permissions
    8. 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 
      

    9. Create a directory with specific owner and group
    10. 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.

    11. Create multiple directories at specific location
    12. 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} 
      

    13. Create a directory and set the SELinux context
    14. 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.

    mkdir mkdir command
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    free Command in Linux (Check Memory Uses)

    A Practical Guide to Extracting Compressed Files in Linux

    TR Command in Linux: A Guide with Practical Examples

    TR Command in Linux: A Guide with Practical Examples

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Setting and Getting the Default Timezone in Python
    • What is Media Access Control (MAC) Address?
    • What is Cross-Site Scripting (XSS)?
    • What is Content Security Policy (CSP)?
    • A User’s Guide to Understanding Redirection Operators in Bash
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.