The Command Line

Shells, paths and the essential commands — navigating, files, pipes and redirection, permissions, processes, environment variables and scripting.

Beginner 3 min readLesson 8 of 8

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 (ls is /bin/ls) plus arguments.
Shell
ls -la /var/log
#^  ^^^ ^^^^^^^^
#|  |   argument
#|  options (flags)
#command
CommandDoes
pwdPrint the current directory
ls -laList files, including hidden ones, with details
cd dir / cd .. / cd ~ / cd -Change directory / up one / home / previous
tree -L 2Show 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

CommandDoes
touch a.txtCreate an empty file (or update its timestamp)
mkdir -p a/b/cCreate directories, including parents
cp src dst / cp -r dir1 dir2Copy a file / a directory
mv old newMove or rename
rm file / rm -r dirDelete (there's no recycle bin!)
cat f / less fPrint a file / page through it
head -n 20 f / tail -f logFirst 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).

||cat access.loggrep 500wc -l> errors.txt
Shell
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 -10

Other 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.

181716150413120100owner rwx = 7group r-x = 5other r-- = 4
chmod 754
Shell
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

Shell
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 suspends

Environment variables and PATH

Environment variables are key–value settings inherited by child processes. PATH lists the directories searched for commands.

Shell
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

Shell
#!/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"
done

Run 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.

Key takeaways
  • 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.
  • PATH decides which programs run; quote variables in scripts and use set -euo pipefail.