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 #

Read an ls permission string
$ 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.

MeaningSymbolicNumeric
Owner everything, others read onlyu=rwx,go=r744
Owner everything, group read and enteru=rwx,g=rx750
Everyone reads, owner writesu=rw,go=r644
Everything for everyonea+rwx777
Both notations in action
$ 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.

Make a whole project private to its owner
$ 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.