mkdir Command in Linux | How to Create Directories

mkdir-command

The mkdir command stands for “make directory,” just as the name signifies, and is used to create directories. Knowing how to create directories makes it easy to organize your files in Linux neatly.

You can perform other tasks when creating the directory using the mkdir command, such as defining the directory’s permission. Moreover, you can create a hierarchical directory. Read on as this post discusses the mkdir command in detail using examples.

Understanding the mkdir Command in Linux

Using the mkdir command follows the following syntax:

The [options] are the optional metrics you include to define how the command should run. We’ve covered the commonly used options in the table below.

OptionsDescription
–helpIt opens up the mkdir command help page.
-vIt adds verbose when running the command.
–versionIt shows the mkdir command version on your system.
-pIt creates a hierarchical directory by creating the parent directories.
-mThe option lets you set the permissions for the directory.

mkdir Command Example Usages

Although we’ve introduced the mkdir Linux command, we still must give examples of how to use it to understand it better. Check out the examples below.

Access the mkdir Command Help Page

You can display the help page to view all the options by adding the –help option.

The help page will appear as shown below.

mkdir-command-help-page

Add Verbose Mode

To display information on every mkdir command you run, include the -v in your command using the format below.

We will visualize the verbose mode in the other examples.

Display mkdir Version

If you want to check the version of the mkdir command available on your system, use the –version option.

mkdir-version

mkdir Command Create a Directory

You can quickly create a directory on your terminal using the mkdir command. Add the name for your directory and any options and your directory will be created.

For instance, we’ve created the Names directory and run the ls command to confirm that our directory has been created in our current working directory.

mkdir-command-create-directory

Creating Multiple Directories

The mkdir command allows you to create multiple files with one command. Once you specify the names for all the directories, they will instantly get created, as in the output below.

mkdir-command-create-multiple-directories

Suppose your naming for the directories you want to create follows a specific pattern. You can use a shorthand to create them quickly.

Assuming your naming begins with ‘Hosts_’ followed by numbers 1 to 5, a better way to create such directories is by running the command below.

All the directories will be created, and we’ve verified them using the ls command.

mkdir-use-pattern-to-create-directories

Creating Parent/Hierarchical Directories

When creating a directory, you can include its parent directories if they don’t exist. This option makes it easy to create a parent and its child sub-directories on different levels.

Read Also: How to check the current working directory in Linux.

For instance, we’ve created three levels using the -p option in the command below.

Thanks to the -v option, the information displayed shows the different levels for every directory created.

mkdir-create-parent-directories

Using the ls -R option, we can view the directory structure we created using the -p option.

ls-command-show-directory-structure

mkdir Command Specify Location for New Directory

Our examples have demonstrated creating a directory in the current working directory. However, you can specify the path on which you wish to have the new directory.

For instance, by specifying the path, we can rerun the previous example and create the parent directories in the /home/linuxgeekery/Downloads/ location.

mkdir-specify-path

Create Directories and Assign Permissions

The mkdir command offers the -m option to set permissions for the new directory. For instance, to give a permission of 755, our command would be as follows:

mkdir-create-directories-with-permissions

We’ve used the ls -l to confirm that the created directory has the set permissions.

verify-set-permissions

Alternatively, we would use the below command to set the permission for all users to rw(read, write).

mkdir-set-directory-permissions

Conclusion

You can’t be an experienced Linux user without knowing how to use the mkdir command to create directories. Luckily, this post shares everything you need to know about creating Linux directories. Practice the presented examples to master how to use the mkdir Linux command quickly.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top