Chat Zalo Chat Messenger Phone Number Đăng nhập
How to Create a Directory in Linux with mkdir Command {Examples}

How to Create a Directory in Linux with mkdir Command {Examples}

What is the mkdir command on Linux?

The mkdir command

on Linux/Unix allows users to create or create new directories. MKDIR stands for “Make Directory”.

With mkdir, you can also set permissions, create multiple directories (folders) at once, and much more.

This tutorial will show you how to use the mkdir command on Linux.

  • Linux or UNIX-like system
  • prerequisites. Access to a terminal/command line
  • .

  • A user with permissions to create and change directory settings

. mkdir command

syntax

in Linux

The basic command for creating directories in Linux consists of the mkdir command and the directory name. Because you can add options to this command, the syntax looks like this:

mkdir [option] dir_name To

better understand how to use mkdir, see the examples we provide in the rest of the guide. How to make a new directory on Linux To create a directory using the terminal, pass the desired name to the mkdir command. In this example, we create a Linux directory on

the

desktop. Remember that Linux commands and options are case sensitive.

mkdir Linux

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

.

To verify, use ls

. How to create multiple directories with mkdir You can

create directories one by one with mkdir

, but this can take a lot of time. To avoid that, you can run a single mkdir command to create multiple directories at once.

To do this, use the square brackets {} with mkdir and enter the directory names, separated by a comma.

mkdir {test1,test2,test3}

Do not add any spaces in curly braces for directory names. If you do, the names in question will include the additional characters:

How to create parent directories

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

For example, if you want to create “dirtest2” in “dirtest1” inside the Linux directory (i.e. Linux/dirtest1/dirtest2), run the command: mkdir -p

Linux/dirtest1/dirtest2

Use ls -R to display the recursive directory tree. 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 Creating a Directory

The mkdir command by default grants RWX permissions only to the current user. To add read, write, and execute permissions for all users, add the -m option with user 777 when you create a directory.

To create a DirM directory with rwx permissions: mkdir -m777 DirM

To list all directories and display permission sets: -l The

directory with

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

How to check directories

When executing mkdir commands, there are no comments for successful operations. To view the details of the mkdir process, add the -v option to the terminal command.

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

When getting the feedback of the process, it is not necessary to run the ls command to verify that the directory was created.

mkdir command options

and

syntax summary Option / Syntax

Description mkdir directory_name mkdir {dir1,dir2,dir3,dir4}not{} mkdir –p directory/path/newdir mkdir –m777 directory_name mkdir -v directory_name(s)

Conclusion

This guide covered all the commands you need to create directories on

Linux.

Now you understand how to use the Linux mkdir command. It is simple and easy to use.

If you have the required permissions, there should be no error messages when you follow the instructions in this article.

Contact US