chmod
Change file mode bits (permissions).
Basic Usage
| Command | Description |
|---|---|
chmod 755 [file] | rwx for owner, rx for group/others. |
chmod 644 [file] | rw for owner, r for group/others. |
chmod +x [file] | Add execute permission for all. |
chmod u+x [file] | Add execute permission for owner only. |
chmod -R 755 [directory] | Recursively change permissions. |
Symbolic Modes
More readable and flexible than octal modes.
| Syntax | Description |
|---|---|
u=rwx,g=rx,o= | Set specific permissions for user, group, others. |
ug+w | Add write permission for user and group. |
o-rwx | Remove all permissions for others. |
a+x | Add execute for all (same as +x). |
g+s | Set Set-Group-ID (SGID) bit. |
u+s | Set Set-User-ID (SUID) bit. |
+t | Set Sticky bit. |
Advanced Examples
Reference Permissions
Apply the same permissions as another file.
chmod --reference=[reference_file] [file]
Conditional Execute
Add execute permission only if it's a directory or already has execute for someone.
chmod -R a+X [directory]
Special Bits (SUID, SGID, Sticky)
| Mode | Octal | Description |
|---|---|---|
| SUID | 4000 | Run as owner. |
| SGID | 2000 | Run as group / New files inherit group. |
| Sticky | 1000 | Only owner can delete files in directory. |
Set SUID.
chmod u+s [file]
chmod 4755 [file]
Set SGID on directory (for shared folders).
chmod g+s [directory]
chmod 2775 [directory]
Set Sticky bit (like /tmp).
chmod +t [directory]
chmod 1777 [directory]
Bulk Changes with Find
Change directories to 755 recursively.
find [directory] -type d -exec chmod 755 {} +
Change files to 644 recursively.
find [directory] -type f -exec chmod 644 {} +
Preserve Root
Fail if recursively operating on /.
chmod --preserve-root -R 777 /
Verbose Output
Show changed files only.
chmod -c -R 755 [directory]