File Permissions — Who May Read, Write and Run
Asha wants the whole garden club to read harvest.csv, only herself to change it, and her harvest-report script to be runnable. On Linux, every file carries a small set of switches that decide exactly this, and ls -l shows them to you.
The rule
Three people, three switches: read 4, write 2, execute 1.
$ ls -l ~/garden
-rw-r--r-- 1 asha garden 181 Jun 4 09:12 harvest.csv
-rwxr-x--- 1 asha garden 420 Jun 4 09:30 harvest-report
drwxr-x--- 2 asha garden 4096 Jun 4 09:00 plotsRead the first column in chunks: one character for the type, then three groups of three.
- rw- r-- r--
│ │ │ └── others (everyone else)
│ │ └──────── group (the file's group: garden)
│ └────────────── user (the owner: asha)
└────────────────── type: - file, d directory, l linkEach group has three switches: r (read), w (write) and x (execute), or a - if the switch is off.
For directories the letters mean something slightly different:
rlets you list the names inside.wlets you create, delete and rename entries inside.xlets you enter the directory and reach the files in it.
Changing permissions with chmod, the symbolic way (who, then +, - or =, then the letters):
$ chmod u+x harvest-report # owner may now run it
$ chmod go-w harvest.csv # group and others lose write
$ chmod o= plots # others get nothingThe number way uses one digit per group, adding read 4, write 2 and execute 1:
$ chmod 644 harvest.csv # rw- r-- r--
$ chmod 750 harvest-report # rwx r-x ---The two styles suit different jobs. Letters are best for a small change to what's already there ("also let the owner run it"). Numbers set all nine switches at once, so they're best when you know exactly what the final result should be. Note that chmod only works on files you own, unless you use sudo.
To change who owns a file, use chown, which normally needs root: sudo chown asha:garden harvest.csv.