1. Overview
In this tutorial, we’ll learn how to create a directory and set permissions in a single command using various techniques.
2. Directory Permission
In Linux, while we construct a user, the system generates a group with a similar name as the user and allocates that user’s files to this group. A user can belong to multiple groups. Besides, users and groups can own directories and files simultaneously.
Linux utilizes access permissions to ensure a secure directory and file access. As we create a directory, Linux allocates default permissions to them. With the mkdir command, we can fulfill the objective of creating a directory.
3. Creating Directory
In a Linux system, the mkdir command constructs directories. It can create single or multiple directories at one go. Besides, the mkdir command can set permissions for the directory that it creates. The term mkdir refers to make directory.
To create a directory using mkdir, we’ve to pass the name of the directory as an argument to the mkdir command:
$ mkdir sample
In the above example, the sample is the name of the directory that we create using the mkdir command.
The ls -l command shall check if the directory has been created by listing contents:
$ ls -l
drwxr-xr-x 2 bhat bhat 64 Oct 28 23:06 sample
In the above example, there are two parts of information that are of particular interest to us are bhat bhat.
From left to right, this means that the user bhat owns this directory, and its owning group is also bhat.
Next, there’s drwxr-xr-x.
These are the access permissions. Here, the first character d refers to a folder. The next characters appear in three groups, each of three, respectively describing the access rights of the owner, the owning group, and then everyone else. In the individual group, the first character is for the read permission(r), then write access (w) and finally the execute (x) access. A dash means that the permission is turned off.
Here, the sample directory has the read(r), write(w), and execute(x) rights for the owner, read(r), and execute(x) rights for the group and execute(x) option for other users.
So, complete permission for all would look like this –**rwxrwxrwx.
We can set the permission for a directory during its creation using octal notation. It is a combination of three numbers by which we can set every combination of access permissions.
The below table demonstrates the equivalent octal notations:
r/w/x | octal
--- | 0
--x | 1
-w- | 2
-wx | 3
r-- | 4
r-x | 5
rw- | 6
rwx | 7
4. Creating Directory and Setting Permissions
Let’s now discuss some common solutions for the problem of creating a directory and setting its permissions.
4.1. Using the mkdir Command
To create a directory and set permission simultaneously in a single command, using the mkdir command is:
$ mkdir -m 777 sample
In the above example, we have created the sample directory with read, write and execute rights for all users.
The ls -l command shall check if the directory has been created by listing contents:
$ ls -l
drwxrwxrwx 2 bhat bhat 64 Nov 1 23:26 sample
The mkdir command with option m sets the mode of the created directory.
Moreover, if the parent directory is non-existent, we can create it along with setting permission:
$ mkdir -p -m 777 /homePrac/sample
In the above example, we have created the sample directory(which is within the homePrac directory) with reading, write and execute rights for all users only for the sample directory.
The ls -l command (from the homePrac directory) shall check if the directory has been created by listing contents:
$ ls -l
drwxrwxrwx 2 bhat bhat 64 Nov 2 22:40 sample
Finally, the mkdir command with option p creates the parent directory homePrac. The mkdir command with option m sets the mode of the created directory.
In the above example, we have created the sample directory with read, write and execute rights for all users.
4.2. Using the install Command
A single command to create a directory and set permission simultaneously using the install command is:
$ install -d -m 777 sample
In the above example, we have created the sample directory with read, write and execute rights for all users.
The ls -l command shall check if the directory has been created by listing contents:
$ ls -l
drwxrwxrwx 2 bhat bhat 64 Nov 4 11:12 sample
The install command with option d refers to the directory. It shall create the directory named sample, which is passed as an argument. The mkdir command with option m sets the mode of the created directory.
5. Conclusion
In this article, we learned how to create a directory and set permissions in a single command with the help of the mkdir and install commands. In addition, we discussed the meaning of setting permissions for a directory.