How to use user groups and permissions to manage files and directories?

Step 1: Create a new user group

If you don’t already have a group that you want to use for managing your files and directories, you’ll need to create one. You can use the groupadd command to create a new group. For example, to create a group called developers, you can run the following command:

1
sudo groupadd developers

Step 2: Add users to the group

Once you have created the group, you can add users to it using the usermod command. For example, to add the user johndoe to the developers group, you can run the following command:

1
sudo usermod -a -G developers johndoe

Step 3: Set the group ownership of files and directories

To give the developers group ownership of a file or directory, you can use the chgrp command. For example, to give the developers group ownership of a directory called project, you can run the following command:

1
sudo chgrp developers /path/to/project

Step 4: Set the permissions on files and directories

Finally, to set the permissions on a file or directory, you can use the chmod command. The permissions are typically represented as a series of 3 digits, where each digit corresponds to the permissions for the owner, group, and others. The permissions include read (4), write (2), and execute (1). For example:

1
2
3
4
5
6
7
8
- 7 = read, write, and execute (4 + 2 + 1)
- 6 = read and write (4 +2)
- 5 = read and execute (4 + 1)
- 4 = read only
- 3 = write and execute (2 + 1)
- 2 = write only
- 1 = execute only

To set the permissions on a directory, you’ll typically want to use the chmod command with the -R option to apply the permissions recursively (i.e., to all files and subdirectories within the directory). For example, to give the developers group read and write permissions on the project directory and all its contents, you can run the following command:

1
sudo chmod -R 770 /path/to/project

In the above example, 7 represents read, write, and execute permissions for the owner; 7 represents the same permissions for the developers group; and 0 means no permissions for others.

That’s it! With these four steps, you can use user groups and permissions to effectively manage your files and directories.