Linux Essentials
The command line every engineer needs — the filesystem hierarchy, navigation and file operations, permissions, processes and signals, pipes and redirection, text processing (grep, sed, awk), networking tools, systemd services, SSH and shell scripting.
Almost every server, container and cloud VM runs Linux. Fluency at the shell makes you faster at debugging, deploying and automating. (For kernel internals see Linux internals.)
The filesystem hierarchy
Everything is under one root / — disks, devices, even process information appear as files.
| Path | Contains |
|---|---|
/bin, /usr/bin | User commands |
/sbin, /usr/sbin | System administration commands |
/etc | Configuration files |
/home/<user> | Users' home directories (~) |
/root | Root user's home |
/var | Variable data: /var/log logs, /var/lib app state |
/tmp | Temporary files (often cleared on reboot) |
/opt | Optional third-party software |
/proc, /sys | Virtual filesystems exposing kernel and process info |
/dev | Device files (/dev/null, /dev/sda) |
/mnt, /media | Mount points |
Navigating and managing files
pwd # where am I?
ls -lah # long listing, all files (incl. hidden), human sizes
cd /var/log cd ~ cd - # go to path / home / previous directory
mkdir -p a/b/c # create nested directories
cp -r src/ backup/ # copy recursively
mv old.txt new.txt # move or rename
rm -r dir/ # delete recursively (no recycle bin! careful with -f)
ln -s /opt/app/current app # symbolic link
find . -name "*.log" -mtime -1 -size +10M # find by name, modified < 1 day, > 10 MB
du -sh * df -h # directory sizes / free disk space
cat file less file head -n 20 file tail -f app.log # view; follow a growing logPermissions
-rwxr-x--- 1 alice devs 4096 Dec 4 10:00 deploy.sh
│└┬┘└┬┘└┬┘ owner group
│ │ │ └── others: no permissions
│ │ └───── group: read + execute
│ └──────── owner: read + write + execute
└────────── type: - file, d directory, l link| Symbol | Number | On a file | On a directory |
|---|---|---|---|
r | 4 | Read contents | List entries |
w | 2 | Modify | Create/delete entries |
x | 1 | Execute | Enter (cd) |
chmod 750 deploy.sh # rwx r-x ---
chmod u+x script.sh # add execute for the owner
chmod 600 ~/.ssh/id_ed25519 # private keys must be owner-only
chown alice:devs file # change owner and group
sudo command # run as root (least privilege: only when needed)Processes and signals
ps aux | grep nginx # list processes
top htop # live view of CPU and memory
pgrep -f "python app.py" # find PIDs by command line
kill 1234 # send SIGTERM (polite: please exit)
kill -9 1234 # SIGKILL (forced; no cleanup — last resort)
command & jobs fg %1 # background a job, list jobs, bring to foreground
nohup ./server & # keep running after logout| Signal | Number | Meaning |
|---|---|---|
SIGTERM | 15 | Request termination — apps should shut down gracefully |
SIGKILL | 9 | Kill immediately; can't be caught |
SIGINT | 2 | Ctrl+C |
SIGHUP | 1 | Terminal closed; many daemons reload config on it |
SIGSTOP / SIGCONT | — | Pause / resume |
Containers and Kubernetes stop apps with SIGTERM, then SIGKILL after a grace period — handle SIGTERM (Processes).
Pipes and redirection
The Unix philosophy: small tools that do one thing, composed with pipes.
cmd > out.txt # stdout to a file (overwrite)
cmd >> out.txt # append
cmd 2> err.txt # stderr to a file
cmd > all.txt 2>&1 # both stdout and stderr
cmd < input.txt # stdin from a file
cmd1 | cmd2 # stdout of cmd1 → stdin of cmd2
cmd | tee out.txt # show and save
cmd > /dev/null # discard# Top 10 IPs hitting the server
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
# Count 5xx responses per minute
grep ' 50[0-9] ' access.log | cut -d: -f2-3 | uniq -cText processing
grep -rn "TODO" src/ # recursive, with line numbers
grep -i -E "error|warn" app.log # case-insensitive, extended regex
grep -v DEBUG app.log # invert match
sed -i 's/old/new/g' config.yml # in-place substitution
sed -n '100,120p' big.log # print a line range
awk -F, '{sum += $3} END {print sum}' sales.csv # sum column 3 of a CSV
cut -d: -f1 /etc/passwd # first field
sort -u uniq -c wc -l # sort unique, count duplicates, count lines
jq '.items[] | {name, status}' resp.json # query JSON
xargs -P4 -n1 curl -sO < urls.txt # run a command per input line, 4 in parallelNetworking tools
curl -sv https://api.example.com/health # HTTP request with verbose headers
curl -X POST -H "Content-Type: application/json" -d '{"a":1}' https://…
ss -tlnp # listening TCP ports and owning processes
ip addr ip route # interfaces and routes
dig example.com +short # DNS lookup
ping traceroute mtr # reachability and path
nc -zv db.internal 5432 # is a port open?More in Network tools.
Services with systemd
Most distributions use systemd to start and supervise services:
# /etc/systemd/system/myapp.service
[Unit]
Description=My API
After=network.target
[Service]
User=myapp
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/server --port 8080
Restart=on-failure
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.targetsudo systemctl daemon-reload
sudo systemctl enable --now myapp # start now and at boot
systemctl status myapp
journalctl -u myapp -f --since "10 min ago" # follow its logsSSH
ssh-keygen -t ed25519 -C "laptop" # create a key pair
ssh-copy-id user@server # install the public key
ssh user@server # log in
scp file.txt user@server:/tmp/ # copy files (or rsync -avz for sync)
ssh -L 5432:localhost:5432 user@bastion # tunnel: local port 5432 → remote DBHarden servers: key-only auth (PasswordAuthentication no), no root login, a bastion host or SSM/IAP instead of public SSH, and keep packages updated.
Shell scripting basics
#!/usr/bin/env bash
set -euo pipefail # exit on error, undefined vars, and failed pipes
ENV="${1:-staging}" # first argument, default "staging"
BACKUP_DIR="/var/backups/$(date +%F)"
if [[ "$ENV" != "staging" && "$ENV" != "prod" ]]; then
echo "usage: $0 [staging|prod]" >&2
exit 1
fi
mkdir -p "$BACKUP_DIR"
for db in users orders; do
echo "Backing up $db…"
pg_dump "$db" | gzip > "$BACKUP_DIR/$db.sql.gz"
done
trap 'echo "failed at line $LINENO" >&2' ERRAlways quote variables ("$file") so spaces don't split them. For anything beyond ~50 lines, consider Python.
- Know the filesystem layout: configs in /etc, logs in /var/log, process info in /proc.
- Permissions are rwx for owner, group and others (chmod 640, 750, 600 for keys).
- Processes get SIGTERM to stop gracefully, SIGKILL as a last resort.
- Compose tools with pipes and redirection; grep, sed, awk, sort, uniq, jq for text.
- systemd manages services; journalctl reads their logs.
- SSH with keys; scripts with set -euo pipefail and quoted variables.