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.sh

Output:

-rwxr-xr-- 1 asha team 120 Oct 16 10:15 report.sh

The 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

LetterOn a FileOn a Folder
r (read)View the contentList the file names
w (write)Change the contentAdd or delete files
x (execute)Run as a programEnter 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 rights

Change 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

NumberMeaningTypical Use
755Owner full, others read and runScripts and programs
644Owner edits, others readDocuments and web files
600Owner onlyPrivate keys and passwords
700Owner only, full accessPrivate folders

Fix a Script That Will Not Run

./backup.sh
bash: ./backup.sh: Permission denied

chmod +x backup.sh
./backup.sh

The 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.txt

The 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
0022

A 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"; fi

Key Takeaways

  • Every file has three groups: owner, group, and others.
  • Three rights exist: read (4), write (2), and execute (1).
  • chmod changes rights with letters or numbers.
  • chmod +x makes a script runnable.
  • Use 600 for private files and 755 for shared scripts.
  • chown changes the owner, and umask sets defaults for new files.