2025-01-24
In Unix-like systems, files and directories have permissions that define who can read, write, or execute them. These permissions are represented using a 3-digit octal system, where each digit corresponds to different user categories: owner, group, and others.
Each permission type is represented by a number:
| Permission | Decimal | Binary |
|---|---|---|
| Read (r) | 4 | 100 |
| Write (w) | 2 | 010 |
| Execute (x) | 1 | 001 |
The values for each user category are added together to form a 3-digit octal number. Each of the three digits represents the permissions for a specific category:
For example:
chmod 755 filename
rwxr-xr-xchmodThe chmod (change mode) command is used to modify the
permissions of a file or directory. Permissions are expressed in either
symbolic or numeric form, and the
command allows you to set or change who can read, write, or execute a
file.
In addition to numeric notation, chmod allows you to
specify which user (owner, group, or others) you want to modify
permissions for using symbolic notation.
u = user (owner)g = groupo = othersa = all (owner, group, others)To modify specific user permissions, you can use the following symbolic operators:
+ = add permission- = remove permission= = set exact permission| Command | Description |
|---|---|
chmod +x filename |
Add execute permission for everyone |
chmod u+x filename |
Add execute permission for the user (owner) |
chmod g-w filename |
Remove write permission for the group |
chmod o=r filename |
Set others to read-only permission |
chmod u+x,g-w filename |
Add execute for the user and remove write for the group |
chmod a+r filename |
Add read permission for everyone (user, group, others) |
chownThe chown command is used to change the
owner and/or group of a file or
directory. It doesn’t directly change the permissions but modifies who
owns the file and which group it belongs to.
| Command | Description |
|---|---|
chown user filename |
Change the owner of the file to the specified user |
chown user:group filename |
Change both the owner and group of the file |
chown :group filename |
Change the group of the file, keeping the owner unchanged |
chown -R user:group directory/ |
Recursively change ownership of all files in a directory |