Reference · Bash · from Lesson 01

Command anatomy & navigation

The compressed version. Built for re-reading and printing, not for learning from cold.

The shape of every command

lsCommand — what to run -lOption — how to run it logsArgument — what to run it on

Looking things up

MoveWhen to use it
man cmdThe offline manual. Space pages down, /word searches, q quits.
cmd --helpQuick summary. Works on Linux/GNU tools; often fails on macOS.
which cmdShows which file actually runs when you type that name.
type cmdLike which, but also reveals shell builtins and aliases.
explainshell.comPaste a whole command; it labels every part. Best tool for decoding someone else's script.

Paths

FormMeans
logs/app.logRelative — starts from your current directory.
/var/www/htmlAbsolute — starts at the top of the disk. Same everywhere. What scripts use.
~Your home folder. ~/Desktop works from anywhere.
.The current directory.
..The parent directory. cd .. goes up one level.
-With cd only: the directory you were in before. A fast toggle.

Getting around

CommandDoes
pwdPrint working directory — where am I?
lsList contents. -l long form, -a include hidden, -h human-readable sizes.
cd dirChange directory. Bare cd returns home.
cat fileDump a whole file to the screen. Fine for short files.
less filePage through a long file. Same keys as man; q quits.
head / tail fileFirst / last 10 lines. tail -f follows a log live.
grep pat filePrint lines matching a pattern. -c counts, -i ignores case, -n shows line numbers.

Terminal survival keys

KeyDoes
Ctrl-CCancel the running command. The universal escape hatch.
Ctrl-DEnd of input / exit the shell.
Ctrl-LClear the screen.
Ctrl-A / Ctrl-EJump to start / end of the line.
TabAutocomplete a name. Press twice to list options. Use constantly — it prevents typos.
Up arrowPrevious command. history lists them all.
qQuit man and less. Worth burning into memory.

macOS vs Linux — where copied commands break

Verified on this machine

Your Mac's tools come from BSD; most servers use GNU. Same names, different flags.

ThingYour MacLinux server
default shellzshusually bash
bash version3.2.57 (2007)5.x
ls --helpfailsworks
sed -ineeds an argument: sed -i ''sed -i alone
declare -A, mapfile, **unavailable (bash 3.2)available

To get a modern bash locally: brew install bash. Not needed yet.

Before you press Enter

Destructive — no undo, no trash

rm deletes permanently. There is no recycle bin. rm -rf dir removes a folder and everything inside without asking.

Habits that save you: run ls on a path before you rm it; use rm -i to be prompted; never paste an rm, sudo, >, or curl … | bash line you can't read aloud. > silently overwrites a file — it is quietly destructive too.