You can create new folders and directories in linux by using command line . Teams to create directories is MkDir. The following describes how to create directories with mkdir. We will also look at the keys used with mkdir and the correct syntax, necessary for the correct operation of the command.

An illustration on how to create a new directory in Linux.
Nusha Ashji © Lifewire

How to create a new directory

The easiest way to create a new directory is to type the following command:

Mkdir <имя_фолдера>

For example, to create a directory named test , open a terminal window go to the folder where you want create new folder, then type:

тест Mkdir
mkdir command in ubuntu

Team cd ~ changes the working directory so that when you type the mkdir command, the folder is created exactly where you want it.

Change the permissions of the new directory

After creating a new folder, set permissions so that only a certain user can access it, or so that some people can edit the files in the folder while others have read-only permissions .

To continue the above example, run the command ls, to see the permissions for a folder named test :

ls -lt
ls -lt command in Ubuntu

Run the ls command in the folder where it is located test folder. If this is your home directory (for example, you didn’t use the cd command), then you don’t need to change the directory here.

You should see something like this (but probably with a few other entries given that there are other folders there):

группа владельцев drwxr-xr-x 2 4096 22 января 10:43 тест

Permissions are drwxrwxr-x , 2 , owner , and Group .

  • Symbol d indicates that test is a directory.
  • The first three letters following the d are the owner permissions for the directory specified in the owner name:
    • G for reading.
    • W for the record.
    • x is meant to be executed (meaning you can access the folder).
  • The next three characters are the group’s permissions for the file specified in the group name. The options are r, w and x. A hyphen means that permission is missing. In the example above, any member of the group can access the folder and view files, but cannot write to the folder.
  • The last characters are the permissions that all users have and they are the same as the permissions of the group.

To change the permissions on a file or folder, use the chmod command. Team chmod allows you to specify three numbers that set permissions:

  • Read = 4
  • Write = 2
  • Run = 1

Add the numbers together for a mixture of permissions. For example, for the read and execute permissions attribute, this number is 5 (4 + 1), and for read and write permissions, this number is 6 (4 + 2).

You must specify three numbers as part of the chmod command. The first number is for owner permissions, the second number is for group permissions, and the last number is for everyone else.

For example, to have the owner have full permissions, for a group that has read and execute permissions and anyone else doesn’t have permissions, type the following:

тест chmod 750

Use the chgrp command to change the name of the group that owns a folder. For example, to create a directory that can be accessed by accountants in a company, first create Accounts groups by typing the following:

учетные записи группы

If you don’t have the correct permission to create a group, use sudo to get more privileges or use su command to switch to an account with valid permissions.

Then change the group for the folder by typing the following:

учетные записи chgrp <имя_фолдера>

For example:

проверка учетных записей chgrp

To grant the owner and everyone else in the account group read, write, and execute permissions, but read-only access to other users, use the following command:

тест chmod 770

How to create a directory and set permissions at the same time

You can create a directory and set permissions on that directory at the same time using the following command:

mkdir -m777 <имя_фолдера>

This command creates a folder that anyone can access. It is rare to create folders with this permission.

Create a folder and any parents you need

You can create a directory structure without creating each individual folder, for example to create folders for music like this:

  • / home / music / rock / Alice Cooper
  • / home / music / rock / queen
  • / home / music / rap / drdre
  • / home / music / jazz / louisjordan

It takes time to create a rock folder for Alice Cooper and the Queen, and for the rest, rap and jazz folders.

Pointing out -p you can create all parent folders on the fly if they don’t exist:

mkdir -p <имя_фолдера>

For example, this mkdir command creates one of the folders listed above:

mkdir -p ~ / музыка / рок / alicecooper

Get confirmation that the directory has been created

By default, the mkdir command does not tell you if the directory was created successfully. Usually, if no errors are shown, you can assume it worked. However, if you want to get more verbose output so you know what was generated, use key -v :

mkdir -v <имя_фолдера>

The output will look like this:

mkdir: созданный каталог / путь / к / каталогу

Use mkdir in shell script

You can also use the mkdir command as part of a shell script, such as a script that accepts a path. When the script is executed it creates a folder and adds one text file named hello .

#! / bin / bash 
mkdir $ @
cd $ @
touch привет

Похожие записи