Changing Permissions (chmod)
The chmod command changes file permissions. It's essential for controlling access to files and making scripts executable.
Basic Usage
chmod mode filename
Two Modes: Symbolic and Numeric
Symbolic Mode
Uses letters and symbols:
chmod u+x file.sh # Add execute for user
chmod g-w file.txt # Remove write for group
chmod o=r file.txt # Set others to read only
chmod a+x script.sh # Add execute for all
| Symbol | Meaning |
|---|---|
u | User (owner) |
g | Group |
o | Others |
a | All (u+g+o) |
+ | Add permission |
- | Remove permission |
= | Set exactly |
Numeric Mode
Uses octal numbers:
chmod 755 script.sh # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod 700 private/ # rwx------
chmod 600 secret.txt # rw-------
Common Permission Sets
| Mode | Permission | Use Case |
|---|---|---|
| 755 | rwxr-xr-x | Executables, directories |
| 644 | rw-r--r-- | Regular files |
| 700 | rwx------ | Private directories |
| 600 | rw------- | Private files |
| 777 | rwxrwxrwx | Open to all (avoid!) |
| 400 | r-------- | Read-only for owner |
Exercise: Make Script Executable
Make a script executable:
Recursive Changes
Change permissions on directory and contents:
chmod -R 755 directory/ # All files and subdirs
chmod -R u+w project/ # Add write to all
chmod Options
| Option | Description |
|---|---|
-R | Recursive (directories) |
-v | Verbose (show changes) |
-c | Report only changes |
--reference | Copy from another file |
Symbolic Examples
# Make executable
chmod +x script.sh # For all
chmod u+x script.sh # Owner only
# Remove permissions
chmod go-w file.txt # Remove write from group/others
chmod o-rwx private.txt # Remove all from others
# Set specific permissions
chmod u=rwx,g=rx,o=r file # Specific for each
Numeric Examples
# Calculate: r=4, w=2, x=1
chmod 754 file # rwxr-xr--
# 7 = 4+2+1 = rwx (user)
# 5 = 4+0+1 = r-x (group)
# 4 = 4+0+0 = r-- (others)
Common Mistakes
# Too permissive (security risk)
chmod 777 file # Everyone can do everything
# Script won't run
./script.sh # Permission denied
chmod +x script.sh # Fix: add execute
# Can't enter directory
chmod 644 mydir/ # No x on directory
chmod 755 mydir/ # Fix: add execute
Key Takeaways
chmodchanges file permissions- Symbolic:
chmod u+x(letters and operators) - Numeric:
chmod 755(octal numbers) - Use
-Rfor recursive changes - 755 for executables/directories, 644 for files

