Mkdir Command in Linux with Examples

Channel: Linux
Abstract: Create multiple directories at specific location – This is very lesser known option for creating multiple directories on different location with singl

mkdir (make directory) is the basic command for Unix-like systems for creating a directory in the file system. This is the frequently used command by Unix/Linux system users. If you are are Linux system user, you must be aware of this command.

This tutorial described to you how to create a directory on Unix/Linux-based systems along with various available options. Let’s discuss the Linux mkdir command with useful examples.

  • Linux cd Command Help and Examples

Syntax

mkdir [OPTION]... DIRECTORY...
Create a New Directory in Linux

Use mkdir followed by the directory name to be created under the current directory.

mkdir newdir 

You can also create a directory under another directory by providing the absolute or relative path.

mkdir /opt/newdir
mkdir Command Examples

Here are some more useful examples of mkdir command on Unix/Linux systems.

  1. Create directory in present working directory – Use the following command to create directory in present working directory. Just use the mkdir command followed by the directory name to be created.
    mkdir newdir 
    
  2. Create directory including parent directory – When creating a directory at other location, the parent directories must be available. If the parent directories are not available it will through error with the above examples.

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

    mkdir -p /opt/backup/newdir 
    

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

  3. Creating multiple directory in single command You can also created multiple directories in a single command.
    mkdir {dir_1,dir_2,dir_3} 
    

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

    mkdir  dir_1 /backup /srv/www 
    
  4. Create directory with parent directory – The default mkdir command will return error if any parent directory doesn’t exits. But you can use the 「-p」 option to instruct the mkdir command to create any parent directory if not exists.
    mkdir -p /var/www/folder1/public 
    
  5. Create directory with specific permissions – Use -m option to to set default permission to a directory during creation time. This is similar to create directory and change permission with chmod command.
    mkdir -m 644 dir1  
    mkdir -m a=rwx dir1 
    mkdir -m u=rw,g=r,o=r dir1 
    
  6. Create multiple directories at specific location – This is very lesser known option for creating multiple directories on different location with single command. The following command will create directories named one, two, three under the mydir directory.
    mkdir -p ./newdir/{one|two|three} 
    
Conclusion

In this tutorial, you have learned about the mkdir command in Linux with examples. If you have any other useful examples, please do share in the comments and we will include them in this tutorial.

Ref From: tecadmin

Related articles