How to Use mkdir Command to Make or Create a Linux Directory

Configurare noua (How To)

Situatie

The mkdir command in Linux/Unix allows users to create or make new directories. mkdir stands for “make directory.”

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

Solutie

Pasi de urmat

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

mkdir [option] dir_name
How to Make a New Directory In Linux
mkdir Linux

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

To verify, use ls.

You can create directories one by one with mkdir, but this can be time-consuming. To avoid that, you can run 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}

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

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 “dirtest2 in “dirtest1 inside the Linux directory (i.e., Linux/dirtest1/dirtest2), run the command:

mkdir –p Linux/dirtest1/dirtest2
 example of creating a new directory in linux

se ls -R to show the recursive directory tree.

Without the -p option, the terminal returns an error if one of the directories in the string does not exist.

error message in terminal: directory does not exist or cant be found

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

example how to create a directory with the mkdir command in terminal

Tip solutie

Permanent

Voteaza

(2 din 7 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?