File Permissions
Prerequisites: Your first script
On Linux and macOS, every file has an owner, a group, and permissions that decide who can read, write, or run it.
Reading permissions
# illustrative
ls -l
-rw-r--r-- 1 ada staff 1024 Apr 22 09:30 notes.txt
-rwxr-xr-x 1 ada staff 512 Apr 22 09:31 deploy.sh
drwxr-xr-x 2 ada staff 4096 Apr 22 09:32 reports
The first column: the first character is the type (- file, d
directory), then three groups of three:
- rwx r-x r-x
│ │ │ └── others (everyone else)
│ │ └──────── group
│ └────────────── owner (user)
└────────────────── type
r = read, w = write, x = execute (for a directory, x means you can
enter it). - means the permission is missing.
Changing permissions: chmod
# illustrative
chmod +x deploy.sh # add execute for everyone
chmod u+x,go-w notes.txt # user +execute; group and others -write
chmod 644 notes.txt # rw-r--r--
chmod 755 deploy.sh # rwxr-xr-x
chmod 600 secrets.env # rw------- : only you can read it
The numeric form adds up r=4, w=2, x=1 for owner, group, and others:
7 = rwx, 6 = rw-, 5 = r-x, 4 = r--.
| Mode | Typical use |
|---|---|
644 |
ordinary files |
755 |
scripts and directories |
600 |
private files: SSH keys, .env secrets |
Owners and sudo
chown user:group file changes the owner. System files belong to the
root user; sudo command runs a single command as root, after asking
for your password.
Common mistakes
chmod 777to "fix" a permission error — it lets anyone change or run the file. Give only the permissions actually needed.- Running everything with
sudo. Use it only for commands that genuinely need administrator rights, and read the command carefully first.