Linux Environment β€” Easy Exam Guide Unit u02 (commands, regex, filters, find, make). The exam mostly asks you to trace a piped command and say what it prints. πŸ”΄ = comes a lot.

Written in simple (B1) English. Read it from top to bottom before the exam.

0How "Linux environment" comes in the exam

The main question is a command-tracing one: you get a small file and a pipeline of egrep / tr / cut / sort (or a find), and you say which lines come out. Sometimes there is a Makefile "what does make X print?" or a redirection question.

The question you will seeHow oftenSection
πŸ”΄ Trace a pipeline: egrep … | tr … | cut … on a given filealmost every examΒ§1–§3
πŸ”΄ find with -regex / -perm / -writable β†’ which files matchoftenΒ§4
🟑 Makefile: which commands run / what does make X printsometimes§5
🟑 Redirection (1> / 2>), permissions/chmodsometimes§6

1Regular expressions πŸ”΄

A regular expression (regex) describes a set of strings. A line matches if any part of it fits the pattern (unless you anchor it).

SymbolMeaning
.any single character (except newline)
[abc] / [a-z]any one of these characters / in this range
[^abc]any one character NOT listed
* / + / ?the thing before, repeated 0+ / 1+ / 0-or-1 times
{n} / {n,m}exactly n / between n and m times
a|ba OR b (alternation)
( )group (so a quantifier applies to the whole group)
^ / $start of line / end of line
\< / \>start of word / end of word
BRE vs ERE β€” the backslash trap
In basic regex (plain grep) you must backslash the special ones: \( \) \{ \} . In extended regex (egrep / grep -E) they are bare: ( ) { } | + ?. So a group is \(...\) in grep but (...) in egrep.
β–Ά Reading examples
^ABC.*     line that starts with ABC
.*h$       line that ends with h
AA(AA)*    an EVEN number of A's (2,4,6,…)   ← "AA" then pairs
ax(xx)+y   "a", then an ODD number of x (3,5,7,…), then "y"
[02468]\>  an even digit at the end of a word

2grep / egrep πŸ”΄

🎯 In the exam
egrep -v "pat" drops matching lines. echo $? after egrep prints 0 if something matched (success), 1 if nothing matched. Watch anchors: -X$ matches a line ending exactly in "-X" β€” a line ending in "-XX" does not match it.

3The filter toolbox πŸ”΄

ToolWhat it does (exact behaviour)
cut -d' ' -f2,4print fields 2 and 4 (delimiter = space). Default delimiter is TAB. cut -c1 = character 1.
tr 'a' 'b'translate a→b. tr -d 'x' deletes x. tr -s 'x' squeezes a run of x into one x.
sortalphabetical. -n numeric, -r reverse, -k2 by field 2, -u unique.
uniqremoves adjacent duplicates (input must be sorted first). -c counts.
wc-l lines, -w words, -c bytes.
head -n K / tail -n Kfirst / last K lines. tail -n +K = from line K to the end.
β–Ά How to trace a pipeline
Work left to right, one stage at a time, writing the surviving lines after each |:
egrep "^1.*[02468]\>" file | egrep "AA(AA)*" | tr -s "A" | cut -d" " -f1
1) keep lines starting with 1 whose number ends in an even digit
2) of those, keep lines with an EVEN run of A (β‰₯2)
3) squeeze the A-run to a single A
4) keep only field 1 (the number)
The trick to head -n 2 | tail -n 1 is "take the 2nd line". sort -k4,4 -k3,3 = sort by field 4, ties broken by field 3.

4find πŸ”΄

find DIR [tests] [actions]
🎯 In the exam (2025-06-12, 2024-02-27)
Apply the tests as AND: a file must pass all of them. So a name may match the -regex but still be dropped by -type f (it's a directory) or by -writable (no write bit) or by a wrong extension. Check every test.

5Makefiles 🟑

target: dependency1 dependency2
<TAB>command
β–Ά Tracing "make X" output
Build the dependency tree, then run depth-first, left to right: build each dependency (and its deps) before the target's own command. So for A: B C D where B: E F and C: G H, G: I, the order of echoes is: E, F, B, then I, G, H, C, then D, then A β†’ EFBIGHCDA.

6Permissions & redirection 🟑

Permissions (chmod octal)

9 bits in 3 groups user / group / others, each rwx. Octal value: r=4, w=2, x=1 (add per group). So rwxr-x--- = 750, and 644 = rw-r--r--. On a directory, x means "you can enter (cd) it", r means "you can list it".

Redirection

cmd < in       stdin from file
cmd > out      stdout to file (overwrite);  >> append
cmd 2> err     stderr (channel 2) to file
foo | bar      foo's stdout β†’ bar's stdin
🎯 In the exam β€” foo | bar 1> A 2> B
The pipe sends foo's stdout into bar's stdin. The 1> and 2> attach to bar (the last command): bar's stdout β†’ A, bar's stderr β†’ B. To throw away stderr, use 2> /dev/null.

7AWK (basics) βšͺ rare

AWK runs pattern { action } for each line. $1 $2 … $NF are the fields, $0 the whole line, NR the line number, NF the number of fields. -F sets the field separator. BEGIN{} runs before, END{} after. Example: awk '/foo/ {print $1, $NF}' prints the first and last field of lines containing "foo".

8One-page trap sheet

  • Regex: *=0+, +=1+, ?=0/1. AA(AA)* = even count; ax(xx)+y = odd count of x. ^/$ anchor.
  • grep BRE needs \( \) \{ \}; egrep (ERE) does not. A line matches on any substring unless anchored.
  • egrep -v = invert. echo $?: 0 = matched, 1 = not.
  • cut default delimiter = TAB (use -d' '). tr -s squeezes; tr -d deletes.
  • sort -n numeric, -k by field. uniq needs sorted input. head/tail -n, tail -n +K.
  • find -regex matches the WHOLE path and is a regex not a glob; tests combine with AND.
  • Make: no-arg builds the FIRST rule; build deps depth-first before the recipe; rebuild only if a dep is newer; .PHONY always runs.
  • chmod octal r=4 w=2 x=1 (750 = rwxr-x---). Directory x = enter, r = list.
  • foo | bar 1>A 2>B: redirections bind to bar (stdoutβ†’A, stderrβ†’B).