Lessons · Terminal · Quick reference
Terminal quick reference
67 topics, one line each, in the order Hone teaches them.
Hone is a place to practise programming. This sheet is the whole Terminal track at a glance: every idea it covers, in the order they are taught, one line each. It is a map rather than a lesson. Read opens the full explanation of an idea; Practise gives you a question on it. Both are free, and reading needs no account at all.
The terminal, without fear · Where am I
where am IThe shell always has a current folder. pwd prints its full path, and every command you type runs from there. Read: Where am I? · Practise where am I
what is in herels lists what is in the current folder. Names that start with a dot are hidden from plain ls; ls -a shows them, and ls -l shows details. Read: What is here? · Practise what is in here
moving between folderscd folder moves you into a folder. cd .. goes up one. cd on its own goes home. cd - goes back to where you were before. Read: Moving around · Practise moving between folders
. and ..Every folder contains two pointers: . means this folder and .. means the one above. They work anywhere a path does. Read: . is here, .. is up · Practise . and ..
~ (home)The shell replaces ~ with your home folder before a command runs, so ~/projects always means the same place whatever folder you are in. Read: ~ is home · Practise ~ (home)
The terminal, without fear · Files and folders
making a foldermkdir name makes a folder. mkdir -p a/b/c makes the whole chain at once and stays quiet if it already exists. Read: Making folders · Practise making a folder
making an empty filetouch name creates an empty file if there is none, and only updates the modified time if there is one. Read: Making an empty file · Practise making an empty file
copying a filecp from to makes a copy and leaves the original alone. A folder needs cp -r, for recursive, or cp refuses it. Read: Copying · Practise copying a file
moving and renamingmv old new renames a file. mv file folder/ moves it into the folder. Either way there is one file afterwards, not two. Read: Moving and renaming are one command · Practise moving and renaming
deleting, with no undorm deletes a file with no trash can and no undo. A folder needs rm -r, which removes everything inside it too. Read: Deleting, for good · Practise deleting, with no undo
The terminal, without fear · Look inside
cat: print it to the screencat prints a whole file. Given two files it prints one after the other, which is where its name, concatenate, comes from. Read: Reading a file · Practise cat: print it to the screen
head and tailhead -n 3 prints the first three lines; tail -n 3 prints the last three. tail -f keeps printing new lines as they arrive. Read: The start and the end of a file · Practise head and tail
counting lines and wordswc -l counts lines, wc -w words, wc -c bytes. It reads a file or whatever is piped into it. Read: Counting lines · Practise counting lines and words
finding the lines that matchgrep word file prints the lines that contain the word. -i ignores case, -n adds line numbers, -r searches a whole folder, -c counts. Read: Finding lines · Practise finding the lines that match
finding files by namefind folder -name 'pattern' walks every folder below and prints the paths that match. ls looks at one folder; find looks at the tree. Read: Finding files by name · Practise finding files by name
The terminal, without fear · Plumbing
the pipe |a | b sends what a prints into b as its input. Small tools joined by pipes do what a program would take an hour to write. Read: The pipe · Practise the pipe |
> (save output)command > file writes the command's output into the file instead of the screen, replacing whatever the file held. Read: Sending output to a file · Practise > (save output)
>> (add to the end)command >> file adds the output to the end of the file and keeps everything that was there. Read: Adding to a file · Practise >> (add to the end)
2> (errors)A command prints normal output on stream 1 and errors on stream 2. > redirects only stream 1; 2> catches the errors; 2>&1 sends both to the same place. Read: Two streams: output and errors · Practise 2> (errors)
&& and ||a && b runs b only if a succeeded. a || b runs b only if a failed. a ; b runs both no matter what. Read: Then, or else, regardless · Practise && and ||
The terminal, without fear · Make it yours
variables and exportNAME=ada sets a variable, with no spaces around the equals sign. $NAME reads it. export NAME hands it to programs started from this shell. Read: Variables, and export · Practise variables and export
$? (exit status)After any command, $? holds its exit status: 0 means success, anything else means failure. &&, || and scripts all read it. Read: Every command reports success or failure · Practise $? (exit status)
making a file runnablels -l shows three groups of r, w and x: owner, group, others. chmod +x adds the execute bit, which is what lets ./script.sh run. Read: Permissions, and making a script runnable · Practise making a file runnable
$(...) (a command's output)$(command) is replaced by what the command printed. $((2 + 3)) does whole-number maths. Read: Using a command's output inside another · Practise $(...) (a command's output)
a script with a shebangA script is a text file of commands with a first line naming its interpreter, #!/usr/bin/env bash. chmod +x it and ./name runs it; $1, $# and "$@" are its arguments. Read: A script: commands in a file, run as one · Practise a script with a shebang
Get the answer out of the file · What a pipe really is
what a pipe carriesA pipe joins one command's standard output straight to the next one's standard input. Nothing is written to disk and nothing is named. Read: What a pipe actually carries · Practise what a pipe carries
each stage sees only the one beforeA pipeline is read left to right and every stage is handed exactly what the stage before it printed -- never the original file. Read: Each stage sees only the one before · Practise each stage sees only the one before
errors do not go down the pipeA plain | carries standard OUTPUT. Errors travel on a second stream and go straight to your screen, past every stage. Read: Errors do not go down the pipe · Practise errors do not go down the pipe
build a pipeline one stage at a timeWrite one command, look at it, add the next. A pipeline you built up is a pipeline you can debug. Read: Build it one stage at a time · Practise build a pipeline one stage at a time
Get the answer out of the file · Find the lines you want
grep -c counts lines, not matches-c reports how many LINES matched. Three matches on one line is still one. Read: grep -c counts lines, not matches · Practise grep -c counts lines, not matches
grep -v: everything but-v keeps the lines that do NOT match. Chain them to strip noise one kind at a time. Read: Everything except · Practise grep -v: everything but
grep -w: whole words onlyA pattern matches anywhere in a line, so grep cat finds concatenate. -w requires a word boundary on both sides. Read: Whole words only · Practise grep -w: whole words only
grep -i: however it was typed-i ignores case, which is usually right for anything a human wrote and wrong when the case IS the fact. Read: However it was typed · Practise grep -i: however it was typed
grep -n: somewhere to go back to-n prefixes each match with its line number and a colon, which is the shape every editor knows how to open. Read: Somewhere to go back to · Practise grep -n: somewhere to go back to
Get the answer out of the file · Take the column you need
cut -d and -f-d says what separates the fields, -f says which ones you want, and fields are counted from 1. Read: Taking one column · Practise cut -d and -f
cut's delimiter is a tabcut's default delimiter is a TAB. A line with no delimiter in it comes through whole rather than empty. Read: The delimiter you did not name · Practise cut's delimiter is a tab
runs of spaces are not fieldscut treats every delimiter as a boundary, so two spaces in a row mean an empty field between them. Read: When the columns are spaces · Practise runs of spaces are not fields
Get the answer out of the file · Put it in an order that means something
sort -n: 9 before 100Plain sort orders character by character, so 105 comes before 12 the way ab comes before b. -n compares them as numbers. Read: sort compares text until you tell it otherwise · Practise sort -n: 9 before 100
sort -k: on which column-k N starts the sort key at field N. It runs to the END of the line unless you name a stop, which is what -k2,2 means. Read: Sorting on a column · Practise sort -k: on which column
sort -u: the distinct values-u sorts and drops repeats in one pass. It answers WHICH values, not how many of each. Read: The distinct values · Practise sort -u: the distinct values
Get the answer out of the file · Count what repeats
uniq only sees neighboursuniq compares each line with the one immediately before it. Nothing else. Read: uniq only sees its neighbour · Practise uniq only sees neighbours
uniq -c: how many of eachuniq -c prints each distinct line once with its count in front, right-aligned in a fixed width. Read: How many of each · Practise uniq -c: how many of each
the ten commonest valuesCut the column, sort so duplicates meet, count them, sort those counts as numbers biggest first, take the top. Read: The ten commonest values · Practise the ten commonest values
wc -l counts newlineswc -l counts newline characters, which is the same as lines except when the last line has no newline at the end. Read: wc counts newlines · Practise wc -l counts newlines
Get the answer out of the file · When the answer looks wrong
when a pipeline prints nothingEvery stage handles empty input gracefully, so an empty result travels all the way to the end without complaint. Read: Nothing is an answer · Practise when a pipeline prints nothing
how many rows, how many thingsCounting lines and counting distinct values are the same shape and different questions. Read: How many rows, how many things · Practise how many rows, how many things
the shell's * and grep's *The shell's * expands to matching FILE NAMES before the command runs. grep's * means none-or-more of the thing before it. Read: Two different stars · Practise the shell's * and grep's *
a space in a variable splits itThe shell splits an unquoted expansion on spaces BEFORE the command runs, so one value becomes two arguments. Read: A space in a variable splits it · Practise a space in a variable splits it
Get the answer out of the file · On a file too big to open
head stops a pipeline earlyhead closes the pipe once it has what it asked for, and everything upstream is told to stop. Read: head stops the pipeline · Practise head stops a pipeline early
get it right on a thousand linesShape questions -- the delimiter, the columns, the casing, the header -- have shape answers, and shape does not need volume. Read: Get it right on a thousand lines · Practise get it right on a thousand lines
a pipeline's code is the last stage's$? after a pipeline holds the LAST command's code. A failure in the middle leaves no trace. Read: A pipeline's exit code is the last stage's · Practise a pipeline's code is the last stage's
the one that worked, written downA pipeline you will need again belongs in a small script with a sentence saying what question it answers. Read: The one that worked, written down · Practise the one that worked, written down
More
$((...)) arithmeticQuestions on Hone; no lesson yet. Practise $((...)) arithmetic
Ctrl+C and Ctrl+DQuestions on Hone; no lesson yet. Practise Ctrl+C and Ctrl+D
cut: one field per lineQuestions on Hone; no lesson yet. Practise cut: one field per line
${VAR:-default}Questions on Hone; no lesson yet. Practise ${VAR:-default}
/dev/nullQuestions on Hone; no lesson yet. Practise /dev/null
echo: printing a lineQuestions on Hone; no lesson yet. Practise echo: printing a line
*.txt globsQuestions on Hone; no lesson yet. Practise *.txt globs
loops over filesQuestions on Hone; no lesson yet. Practise loops over files
man pagesQuestions on Hone; no lesson yet. Practise man pages
PATHQuestions on Hone; no lesson yet. Practise PATH
quoting variablesQuestions on Hone; no lesson yet. Practise quoting variables
set -eQuestions on Hone; no lesson yet. Practise set -e
Tab completionQuestions on Hone; no lesson yet. Practise Tab completion
sort | uniq -cQuestions on Hone; no lesson yet. Practise sort | uniq -c
xargs: turning a list into argumentsQuestions on Hone; no lesson yet. Practise xargs: turning a list into arguments