The Command Line
Shells, paths and the essential commands — navigating, files, pipes and redirection, permissions, processes, environment variables and scripting.
The command line (terminal, shell) is a text interface to the operating system. It's how you'll run builds, use git, connect to servers, read logs and automate repetitive work. Servers usually have no GUI at all, so fluency here is a real productivity multiplier.
Terminal, shell, command
- The terminal is the window that displays text (Windows Terminal, iTerm2, the VS Code panel).
- The shell is the program that reads your commands and runs them:
bash,zsh,fish, PowerShell. - A command is usually just a program on disk (
lsis/bin/ls) plus arguments.
ls -la /var/log
#^ ^^^ ^^^^^^^^
#| | argument
#| options (flags)
#commandNavigating the filesystem
| Command | Does |
|---|---|
pwd | Print the current directory |
ls -la | List files, including hidden ones, with details |
cd dir / cd .. / cd ~ / cd - | Change directory / up one / home / previous |
tree -L 2 | Show a directory tree two levels deep |
Paths are absolute (/home/ada/project, starting from the root /) or relative to the current directory (src/main.py, ../docs). . is the current directory, .. its parent, ~ your home.
Working with files
| Command | Does |
|---|---|
touch a.txt | Create an empty file (or update its timestamp) |
mkdir -p a/b/c | Create directories, including parents |
cp src dst / cp -r dir1 dir2 | Copy a file / a directory |
mv old new | Move or rename |
rm file / rm -r dir | Delete (there's no recycle bin!) |
cat f / less f | Print a file / page through it |
head -n 20 f / tail -f log | First lines / follow a growing log |
find . -name "*.py" | Find files by name |
grep -rn "TODO" src/ | Search file contents recursively |
Pipes and redirection
The Unix philosophy: small programs that each do one thing, connected by pipes. Every process has three standard streams: stdin (0), stdout (1) and stderr (2).
cmd > out.txt # write stdout to a file (overwrite)
cmd >> out.txt # append
cmd 2> err.txt # write stderr to a file
cmd > all.txt 2>&1 # both streams to one file
cmd < input.txt # read stdin from a file
cmd1 | cmd2 # stdout of cmd1 → stdin of cmd2
# Top 10 IP addresses in a web server log
cut -d' ' -f1 access.log | sort | uniq -c | sort -rn | head -10Other handy tools: sort, uniq, wc, cut, tr, sed (stream editing), awk (column processing), xargs (turn input lines into arguments), jq (query JSON).
Permissions
ls -l shows something like -rwxr-xr--: the type, then read/write/execute for the owner, the group and others.
chmod +x script.sh # make executable
chmod 644 file # rw-r--r--
chown ada:dev file # change owner and group
sudo cmd # run as the superuser (root)Processes
ps aux | grep node # list processes
top / htop # live view of CPU and memory
kill 1234 # ask process 1234 to stop (SIGTERM)
kill -9 1234 # force-kill (SIGKILL) — last resort
cmd & # run in the background
jobs; fg %1 # list background jobs; bring one to the foreground
Ctrl+C # interrupt (SIGINT); Ctrl+Z suspendsEnvironment variables and PATH
Environment variables are key–value settings inherited by child processes. PATH lists the directories searched for commands.
echo $HOME
export API_URL=https://api.example.com # set for this shell and its children
API_KEY=abc node app.js # set for one command only
echo $PATH
which python # which executable will run?A first shell script
#!/usr/bin/env bash
set -euo pipefail # stop on errors, unset variables and pipe failures
backup_dir="backups/$(date +%F)"
mkdir -p "$backup_dir"
for f in *.db; do
cp "$f" "$backup_dir/"
echo "backed up $f"
doneRun it with chmod +x backup.sh && ./backup.sh. Always quote variables ("$f") so file names with spaces don't break.
Windows
PowerShell is the modern Windows shell; it passes objects between commands rather than text. Many Unix commands have aliases there (ls, cat, cd), and WSL (Windows Subsystem for Linux) gives you a real Linux shell on Windows.
- The shell runs programs with arguments; paths are absolute or relative.
- Pipes (
|) connect small tools; redirect with>,>>,2>,<. - Permissions are rwx for owner, group and others (octal, e.g. 755).
- Manage processes with
ps,top,kill,&,fg. PATHdecides which programs run; quote variables in scripts and useset -euo pipefail.