chmod

Who can read, write and run a file: chmod in letters (u+x) and numbers (755), with a calculator.

>_ Try it: a simulated Linux, right here
Loading the terminal…

Examples

  • chmod +x deploy.sh
  • ./deploy.sh
  • chmod 644 deploy.sh
  • chmod u=rwx,go=rx deploy.sh
  • chmod 600 deploy.sh
  • chmod -R 755 site

How it works

Every file has three sets of permissions: the owner (u), the group (g) and everyone else (o). Each set can read (r), write (w) and execute (x). ls -l shows them as rwxr-xr-x.

Numbers are a shortcut: r = 4, w = 2, x = 1. Add them up for each set: rwx = 7, r-x = 5, so 755. And 644 is rw- r-- r--: the owner writes, everyone reads.

Syntax

chmod [OPTION] MODE FILE...

Options

OptionWhat it does
u g o awho: user (owner), group, others, all
+ - =add, remove, or set exactly
r w xread, write, execute
755the same in numbers (owner, group, others)
-Ra folder and everything inside it

Good to know

  • chmod 777 gives everyone everything: almost never the fix you want.
  • A script needs x to run with ./; a folder needs x to be entered with cd.