Bash File Permissions
File permissions decide who can read, change, or run a file. Linux and macOS protect every file with a small set of rules. Understanding them fixes the famous "Permission denied" message and keeps your data safe.
A House Key Analogy
Picture a house with three groups of people: the owner, the family, and the visitors. The owner holds every key. Family members hold some keys. Visitors hold very few. File permissions follow the same three-group idea.
+----------+----------+----------+ | Owner | Group | Others | | (user) | (family) | (visitor)| +----------+----------+----------+ | r w x | r - x | r - - | +----------+----------+----------+ read read read write execute execute
Read the Permission Text
The ls -l command shows permissions on the left side:
ls -l report.shOutput:
-rwxr-xr-- 1 asha team 120 Oct 16 10:15 report.shThe first ten characters break down like this:
- rwx r-x r-- | | | | | | | +--> Others: read only | | +---------> Group: read and execute | +----------------> Owner: read, write, execute +----------------------> File type (- file, d folder)
The Three Permission Letters
| Letter | On a File | On a Folder |
|---|---|---|
| r (read) | View the content | List the file names |
| w (write) | Change the content | Add or delete files |
| x (execute) | Run as a program | Enter the folder |
Change Permissions With Letters
The chmod command changes permissions. The letters u, g, and o stand for user, group, and others. The letter a means all three.
chmod u+x script.sh # give the owner execute rights
chmod g-w notes.txt # remove write from the group
chmod o-r secret.txt # remove read from others
chmod a+r public.txt # give everyone read rightsChange Permissions With Numbers
Each permission carries a number value: read is 4, write is 2, and execute is 1. Add the values for each group to form one digit.
rwx = 4 + 2 + 1 = 7 r-x = 4 + 0 + 1 = 5 r-- = 4 + 0 + 0 = 4 --- = 0
Three digits describe owner, group, and others in order:
chmod 755 script.sh # rwxr-xr-x
chmod 644 notes.txt # rw-r--r--
chmod 600 private.txt # rw-------
chmod 700 mydir # rwx------Popular Permission Sets
| Number | Meaning | Typical Use |
|---|---|---|
| 755 | Owner full, others read and run | Scripts and programs |
| 644 | Owner edits, others read | Documents and web files |
| 600 | Owner only | Private keys and passwords |
| 700 | Owner only, full access | Private folders |
Fix a Script That Will Not Run
./backup.sh
bash: ./backup.sh: Permission denied
chmod +x backup.sh
./backup.shThe first attempt fails because the file lacks the execute permission. The chmod +x command adds it and the script starts.
Change Permissions in a Whole Folder
The -R flag applies a change to every item inside a folder:
chmod -R 755 project/Use this option carefully. A wrong number can lock you out of your own files or expose private data.
Change the Owner and Group
The chown command assigns a new owner. It needs administrator rights.
sudo chown asha report.txt
sudo chown asha:team report.txtThe second line sets both the owner and the group.
Default Permissions With umask
New files receive starting permissions based on a value called umask. The value removes rights from the maximum setting.
umask
0022A umask of 022 gives new files 644 and new folders 755. A umask of 077 limits new files to the owner only.
Check Permissions Inside a Script
#!/bin/bash
file=$1
if [ -r "$file" ]; then echo "Readable"; fi
if [ -w "$file" ]; then echo "Writable"; fi
if [ -x "$file" ]; then echo "Executable"; fiKey Takeaways
- Every file has three groups: owner, group, and others.
- Three rights exist: read (4), write (2), and execute (1).
chmodchanges rights with letters or numbers.chmod +xmakes a script runnable.- Use 600 for private files and 755 for shared scripts.
chownchanges the owner, andumasksets defaults for new files.
