Linux & the Shell
Learn to live in a terminal: move around the file tree, chain small tools into pipelines, see and control what is running, and turn it all into scripts that fail loudly. Every command here is real and was run to check its output.
- Beginner
- 4 units, 12 lessons, 148 exercises
- Shell
- No experience needed
Unit 01 · 3 lessons
Files and folders
Find your way around the file tree and change it without a mouse
- 01Where am I
pwd, cd and ls: absolute and relative paths
- 02Make, move, remove
mkdir, cp, mv, rm, and how the shell expands * before a command runs
- BOSSPermission denied
Read ls -l, fix a script that will not run, and learn who may do what
Read the unit guide
Linux keeps everything in one tree that starts at /, the root. There are no drive letters. A path that starts with / is absolute: it means the same thing from anywhere. Any other path is relative: it starts from the folder your shell is standing in, which pwd prints and cd changes.
Three shortcuts are worth knowing cold. . is the folder you are in, .. is its parent, and ~ is your home folder. Names that start with a dot are hidden: ls skips them and ls -a shows them.
To change the tree: mkdir -p makes a folder and any missing parents, touch makes an empty file, cp -r copies a folder, mv moves or renames, and rm -r deletes a folder with everything inside. There is no trash and no undo.
The shell rewrites your line before the command sees it. A pattern like *.log is replaced by the matching names, and the line is split at spaces, so a name with a space needs quotes. Most surprises at a terminal come from forgetting one of these two steps.
Every file has an owner, a group, and nine permission bits: read, write and execute for the owner, for the group, and for everyone else. Read them with ls -l, change them with chmod, either in octal (r=4, w=2, x=1, so 755) or by name (u+x).
On a folder the bits mean something different: r lists the names, w creates and deletes entries, x lets you pass through. That is why deleting a file depends on the folder it sits in, not on the file.
Unit 02 · 3 lessons
Text and pipes
Join small tools into pipelines that answer questions about text
- 01Three streams
stdout, stderr and stdin, and how >, >>, 2> and | route them
- 02Filters
grep, sort, uniq, cut, tr, and a first taste of sed and awk
- BOSSThe top talkers
Build sort | uniq -c | sort -rn | head over an access log, one stage at a time
Read the unit guide
Every command has three streams. stdin (0) is its input, stdout (1) carries its results, and stderr (2) carries its complaints. Both outputs land on your terminal unless you send them elsewhere: > writes stdout to a file and replaces it, >> adds to the end, 2> does the same for stderr, and < feeds a file to stdin.
The shell sets up redirections before the command starts. That is why sort f > f empties the file it was about to read, and why a failed command still leaves an empty output file behind.
A pipe, a | b, connects the stdout of one command to the stdin of the next. Only stdout travels through it. Unix tools are built for this: each does one small job on lines of text, and you chain them.
The filters to know: grep keeps lines that match (-i ignore case, -v invert, -c count, -r search a folder, -n line numbers), cut picks columns, tr swaps characters, sed 's/a/b/' replaces text, awk '{print $2}' prints a field, sort reorders lines (-n by number, -r reversed), and head, tail and wc -l trim and count.
The idiom worth memorising is sort | uniq -c | sort -rn | head: how often does each value appear, most common first. uniq only merges lines that sit next to each other, so the first sort is not optional.
Two adapters finish the kit. xargs turns lines on stdin into arguments for commands that do not read stdin, like rm. tee copies stdin to a file and passes it on, so you can save the middle of a pipeline.
Unit 03 · 3 lessons
Processes
See what is running, stop it politely, and know where commands come from
- 01What is running
ps, top and PIDs, then exit status with $?, && and ||
- 02Signals and jobs
Ctrl-C, kill and kill -9, background jobs, and why TERM comes first
- BOSSCommand not found
Environment variables and PATH, ~/.bashrc, then services with systemctl
Read the unit guide
A process is a running program. The kernel gives each one a number, its PID, and remembers which process started it. ps aux prints a snapshot of all of them, top shows the list live, and pgrep name finds PIDs by name. Whatever you type at a prompt becomes a child of your shell.
Every command ends with an exit status: 0 for success, anything else for failure. $? holds the status of the most recent command. a && b runs b only if a succeeded and a || b only if a failed, which is how one-line safety checks are written.
Signals are how processes are told to stop. Ctrl-C sends INT, kill PID sends TERM, and both can be caught by the program so it can clean up. kill -9 sends KILL, which the program never sees: the kernel removes it mid-step. Try TERM first. A process ended by signal N reports status 128 + N.
The shell tracks what it started as jobs. cmd & runs in the background, Ctrl-Z pauses the foreground job, bg and fg move jobs between the two, and nohup lets a command outlive the terminal that started it.
Each process carries an environment: named strings such as HOME and PATH. export marks a shell variable to be copied into the programs you start. PATH is the list of folders the shell searches for a command, left to right, first match wins. "command not found" almost always means the program's folder is not on that list. Changes last only for the current shell unless you put them in ~/.bashrc.
Long-running programs are run as services by systemd. systemctl status|start|stop|restart name acts now, enable makes it start at boot, and journalctl -u name shows what it logged.
Unit 04 · 3 lessons
Shell scripts
Turn a pipeline into a tool that takes arguments, makes decisions and fails loudly
- 01Variables and quoting
name=value, the two kinds of quotes, $(command), $1 and $#
- 02Tests and loops
if and [ ], for and while read, functions and exit codes
- BOSSA backup script that fails loudly
set -euo pipefail, quoting disasters, find, tar and cron
Read the unit guide
A script is a file of commands with a shebang, #!/bin/bash, on its first line and the execute bit set. Variables are set with name=value, with no spaces around the equals sign, and read with $name. $(command) captures a command's output, and $((a + b)) does arithmetic.
Quotes decide what the shell rewrites. Inside double quotes $name expands; inside single quotes nothing does. An unquoted variable is split at its spaces into separate words, and an empty one vanishes altogether. So the rule is simple: put double quotes around every "$variable".
A script reads its arguments as $1, $2 and so on. $# counts them, "$@" is all of them, and $? is still the exit status of the last command.
if runs a command and branches on its exit status. [ ... ] is just such a command: -f and -d test for files and folders, -z for an empty string, = compares text, and -eq, -lt, -gt compare numbers. for f in *.log walks a list of words, and while read line takes a file one line at a time.
By default a script ignores failures and runs the next line, which is how a backup script deletes or archives the wrong thing. Start every script with set -euo pipefail: stop when a command fails, treat an unset variable as an error, and let a pipeline fail when any stage fails.
For jobs across a folder tree, find prints matching paths at any depth, and -exec or xargs runs a command on them. tar -czf packs a folder into one compressed file, and a line in cron runs your script on a schedule, with a bare environment, so use full paths.
Learn Linux & the Shell on your phone.
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.