chmod changes file permissions — who can read, write, and execute what. It looks cryptic the first week (why 644?) and obvious by the second, because the whole system is three bits per person.
Reading permissions #
$ ls -l deploy.sh
-rwxr-xr-- 1 ana ana 512 Oct 21 09:00 deploy.sh
The first ten characters split into: file type, then three triplets — owner (rwx), group (r-x), others (r--). r is read, w write, x execute.
Two ways to write the same thing #
Symbolic mode says who gets what; numeric mode says everything at once. Each triplet is a number: r=4, w=2, x=1, added up.
| Meaning | Symbolic | Numeric |
|---|---|---|
| Owner everything, others read only | u=rwx,go=r | 744 |
| Owner everything, group read and enter | u=rwx,g=rx | 750 |
| Everyone reads, owner writes | u=rw,go=r | 644 |
| Everything for everyone | a+rwx | 777 |
$ chmod u+x deploy.sh
$ chmod 644 config.yaml
Recursion and ownership #
-R applies changes down a whole tree. Ownership itself is a different command, chown — sudo chown ana:ana file — which needs administrator rights, while chmod usually does not.
$ chmod -R u+rwX,go-rwx ~/projects/private
The capital X sets execute only on directories and files that are already executable — the correct sweep almost every time.
Permissions travel with files, so archives made with tar preserve them — handy when staging deployments. Remote servers have their own permission world: see ssh. More in the journal.
