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 see | How often | Section |
|---|---|---|
π΄ Trace a pipeline: egrep β¦ | tr β¦ | cut β¦ on a given file | almost every exam | Β§1βΒ§3 |
π΄ find with -regex / -perm / -writable β which files match | often | Β§4 |
π‘ Makefile: which commands run / what does make X print | sometimes | Β§5 |
π‘ Redirection (1> / 2>), permissions/chmod | sometimes | Β§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).
| Symbol | Meaning |
|---|---|
. | 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|b | a OR b (alternation) |
( ) | group (so a quantifier applies to the whole group) |
^ / $ | start of line / end of line |
\< / \> | start of word / end of word |
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.^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 π΄
grepprints lines that contain a match.egrep=grep -E= extended regex.- Useful options:
-v(invert: lines that do not match),-i(ignore case),-n(line numbers),-e(give a pattern; repeat for several),-w(whole word),-c(count).
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 π΄
| Tool | What it does (exact behaviour) |
|---|---|
cut -d' ' -f2,4 | print 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. |
sort | alphabetical. -n numeric, -r reverse, -k2 by field 2, -u unique. |
uniq | removes adjacent duplicates (input must be sorted first). -c counts. |
wc | -l lines, -w words, -c bytes. |
head -n K / tail -n K | first / last K lines. tail -n +K = from line K to the end. |
|:
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]
-name "*.c"matches the file name only (glob).-iname= ignore case.-regex "β¦"matches the whole relative path (e.g../sub/file), and it is a regex, not a glob β use".*\.c", not"*.c". Add-regextype posix-extendedfor extended regex.-type f/d/l= regular file / directory / symlink.-perm 644= exactly these permissions.-writable/-readable/-executable= by you.-size +500c= bigger than 500 bytes (c=bytes,k/M/G;+=more,-=less).-maxdepth 1= this directory only.-exec cmd {} \;= run cmd on each match.
-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
- A rule has a target, its dependencies, and a recipe (commands). Each recipe line must start with a real TAB.
makewith no argument builds the FIRST rule in the file.make Xbuilds target X.- To build a target, make first builds its dependencies (going down the graph), then runs the recipe β but only if a dependency is newer than the target.
.PHONY: cleanmarks a target as an action (not a real file) so it always runs.- Variables:
$(CC). Automatic:$@= target,$^= all deps,$<= first dep.make -n= print commands without running them.
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
foo | bar 1> A 2> B1> 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.cutdefault delimiter = TAB (use-d' ').tr -ssqueezes;tr -ddeletes.sort -nnumeric,-kby field.uniqneeds sorted input.head/tail -n,tail -n +K.find -regexmatches 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;
.PHONYalways 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).