How to Create Directories in Linux using mkdir Command

How to Create Directories in Linux using mkdir Command

Create directory in linux. In this tutorial, you will learn how to create directories in linux uisng The mkdir command with command prompt or terminal.

Note that, The mkdir command allows users to create or make new directories in linux system.

This tutorial guide will help you to create directories in linux system using mkdir command and you can also set permissions, create multiple directories (folders) at once, and much more.

Prerequisites

  • Linux or UNIX-like system.
  • Access to a terminal/command line.
  • A user with permissions to create and change directory settings.

Syntax of mkdir Command in Linux

First of all, you need to know the basic command for creating directories in Linux consists of the mkdir command and the name of the directory. as shown below:

mkdir [option] dir_name

How to Make a New Directory In Linux

To create a directory using the mkdir command in linux with terminal.

See the following example of how to create directory in linux:

mkdir Linux

If the operation is successful, the terminal returns an empty line.

To verify, use ls.

Note: To create a hidden directory, follow our guide on how to show and create hidden files in Linux.

How to Create Multiple Directories with mkdir

If you want to create multiple directory at once command. So, you can use a single mkdir command to create multiple directories at once.

To do so, use the curly brackets {} with mkdir and state the directory names, separated by a comma.

mkdir {test1,test2,test3}

Note that, Do not add any spaces in the curly brackets for the directory names.

How to Make Parent Directories

Building a structure with multiple subdirectories using mkdir requires adding the -p option. This makes sure that mkdir adds any missing parent directories in the process.

For example, if you want to create “mydirtest2 in “mydirtest1 inside the Linux directory (i.e., Linux/mydirtest1/mydirtest2), run the command:

mkdir –p Linux/mydirtest1/mydirtest2

Note that, you can use ls -R to display the recursive directory tree in linux. And Without the -p option, the terminal returns an error if one of the directories in the string does not exist.

How to Set Permissions When Making a Directory

The mkdir command by default gives rwx permissions for the current user only.
To add read, write, and execute permission for all users, add the -m option with the user 777 when creating a directory.

To create a directory DirM with rwx permissions:

mkdir –m777 DirM

To list all directories and show the permissions sets: -l

The directory with rwx permissions for all users is highlighted. As you can see on the image above, two other directories by default have rwx permission for the owner, xr for the group and x for other users.

How to Verify Directories

When executing mkdir commands for creating a directory in linux, there is no feedback for successful operations. To see the details of the mkdir process, append the -v option to the terminal command.

Let’s create a Details directory inside Dir1 and print the operation status:

mkdir Command Options and Syntax Summary

Option / SyntaxDescription
mkdir directory_nameCreates a directory in the current location
mkdir {dir1,dir2,dir3,dir4}Creates multiple directories in the current location. Do not use spaces inside {}
mkdir –p directory/path/newdirCreates a directory structure with the missing parent directories (if any)
mkdir –m777 directory_nameCreates a directory and sets full read, write, execute permissions for all users
mkdir –v directory_name(s)Creates a directory in the current location

Conclusion

This guide covered all commands you have learned how to create directories in Linux and how to set permission for directory.

Recommended Posts

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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