Command Line Basics (Linux and macOS)
Permissions and Getting Help
In this lesson you will
- Read a permissions listing
- Change permissions with chmod
- Use man and --help to learn commands
Introduction
On Linux and macOS, every file says who may read it, change it or run it. This protects your system, and it explains many "permission denied" errors.
Reading permissions
Run ls -l and look at the start of each line:
-rwxr-xr-- 1 sam staff 120 Nov 6 10:00 script.sh
drwxr-xr-x 2 sam staff 64 Nov 6 09:00 projectsThe first character shows the type: - for a file and d for a directory. The next nine characters are three groups of three:
- Owner (user), group, and others
- In each group:
rread,wwrite,xexecute (run)
So rwxr-xr-- means the owner can read, write and run, the group can read and run, and everyone else can only read.
Quick quiz
What does rwxr-xr-- mean for people who are not the owner or in the group?
Changing permissions with chmod
Terminal
$ chmod +x script.sh # make it runnable
$ chmod -w notes.txt # remove write permission
$ chmod 644 notes.txt # owner read and write, others readThe numbers add up as read 4, write 2, execute 1. So 6 is read plus write, and 7 is all three. Use the least permission needed. Avoid chmod 777, which lets everyone do everything.
Running as administrator
Some tasks need extra rights. sudo runs one command as the administrator:
Terminal
$ sudo apt updateIt asks for your password. Only use sudo with commands you understand, because it can change or delete important system files.
Getting help
You do not need to memorise everything. Ask the computer:
Terminal
$ ls --help # short summary of options
$ man ls # the full manual page; press q to quitSearching the web for the exact error message also works well. Read answers carefully before pasting commands.
Try it yourself
Create a file, run ls -l and read its permissions. Remove write permission with chmod -w, then try to add a line with echo "hi" >> file. What happens? Restore it with chmod +w.
Summary
- Permissions control who can read, write and execute each file.
chmodchanges permissions, andsudoruns a command as administrator.- Use
--helpandmanto learn what a command can do.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading