diff --git a/Week 1 - Linux Fundamentals/03_Users_and_Permissions/file-permissions.md b/Week 1 - Linux Fundamentals/03_Users_and_Permissions/file-permissions.md new file mode 100644 index 0000000..00feeae --- /dev/null +++ b/Week 1 - Linux Fundamentals/03_Users_and_Permissions/file-permissions.md @@ -0,0 +1,101 @@ +# ๐Ÿ“ File Permissions in Linux + +File permissions help control **who** can read, write, or execute a file or directory. + +## ๐Ÿ” Symbolic Permissions + +User types: + +- `u` โ†’ **user** (owner) +- `g` โ†’ **group** +- `o` โ†’ **others** + +Permissions: + +- `r` โ†’ **read** +- `w` โ†’ **write** +- `x` โ†’ **execute** + +Operators: + +- `+` โ†’ **add** permission +- `-` โ†’ **remove** permission +- `=` โ†’ **set exactly** (override) + +### ๐Ÿงช Examples + +```bash +# Add execute permission for user +chmod u+x script.sh + +# Remove write permission from group +chmod g-w file.txt + +# Set read-only for everyone. `a` means all (user, group, others) +chmod a=r file.txt + +# Custom permission set for user, group, others +chmod u=rw,g=r,o= file.txt +``` + +## ๐Ÿ—‚๏ธ Numeric Permissions + +Each permission has a number: + +- `r` = **4** +- `w` = **2** +- `x` = **1** + +To calculate total: + +- `7` = 4 + 2 + 1 โ†’ **read + write + execute** +- `6` = 4 + 2 โ†’ **read + write** +- `5` = 4 + 1 โ†’ **read + execute** +- `0` = **no permissions** + +**Permissions** are defined in **three digits: user-group-others** (755, 644, etc.). + +### ๐Ÿงช Examples + +```bash +# rwx for user, rx for group and others +chmod 755 script.sh + +# rw for user, r for group and others +chmod 644 notes.txt + +# full access for user only +chmod 700 secret.sh +``` + +## ๐Ÿ‘ค Ownership + +Every file or directory has an owner and a group. To view ownership `ls -l` command is used. + +To change owner: + +```bash +sudo chown username file.txt +``` + +To change group: + +```bash +sudo chgrp groupname file.txt +``` + +To change both owner and group: + +```bash +sudo chown username:groupname file.txt +``` + +### ๐Ÿงช Examples + +```bash +# Change owner to alice +sudo chown alice document.txt + +# Change owner to bob and group to developers +sudo chown bob:developers script.sh +```