Context:FIT1043_MOC, FIT2014_MOC, FIT2109_MOC Β· locate β navigate β inspect β search/count β sort β cut columns β pipe β compress β awk β sed/tr/regex β permissions/streams/status β hand off Β· depth in Unix Shell (Bash); text-transform detail in Text Processing with sed and tr; execution mechanics in Shell Execution Model (Permissions, Processes, Streams) Β· FIT2014 = Lab 0 tooling Β· FIT2109 = W1 systems angle
Read protocol: scan tables β attempt the practice blank β follow the pattern-note link only where you failed.
Quick Revision
π― Objective: explore/clean a huge text/CSV file from the command line without loading it into memory β chain small tools with pipes, then hand the reduced file to R/Python.
β‘ Key Constraint: the pipe | is buffered + line-at-a-time β memory stays bounded so it scales past RAM; >overwrites a file, |feeds the next program β never confuse them.
- file, d directory; a - in a slot = permission absent
rwx
read Β· write Β· execute
on a directory, x means traverse (needed to cd in)
chmod
chmod u+x file
add execute for owner; changes metadata only, never file contents
ls -l
ls -l file
β run this before guessing why a script wonβt run
$PATH
echo $PATH
colon-separated dirs, scanned left to right; . is not on it
bare name
run.sh
a lookup request β command not found if not on $PATH
explicit path
./scripts/run.sh
contains / β skips lookup, used literally
builtin vs external
cd (builtin) vs ls (program on disk)
cd must be builtin β it changes the shellβs own cwd
shebang
#!/usr/bin/env bash
script runs in Bash whatever your interactive shell is
ps / $$
ps Β· echo $$
process status (pid, ppid, comm) Β· PID of the current shell
exit status
echo $?
0 = success, nonzero = failure β independent of what was printed
Three signature errors = three stages β command not found (lookup) Β· permission denied (found, but no x) Β· no such file or directory (path resolves to nothing). The message is the diagnosis.
Key move:grep -c counts in one step; > persists the filtered rows (use >> to append instead of overwrite).
Practice 3: From app.log, save the six most frequent ERROR messages to top_errors.txt while keeping any error output from the pipeline on screen β then prove the pipeline succeeded.
Reference solution
grep "ERROR" app.log | sort | uniq -c | sort -nr | head -n 6 > top_errors.txtecho $? # 0 -> but this is HEAD's status, not grep'sset -o pipefail # (in a script) make any failing stage propagate
Key move: four tools combined β grep filters, sort makes duplicates adjacent for uniq -c, sort -nr ranks by count, head truncates. > retargets stdout only, so stderr stays visible; and $? reports only the last stage unless pipefail is set.
Practice 4: ./tools/clean.sh prints Permission denied. Diagnose it in two commands and fix it in one.
Reference solution
pwd # 1. am I where I think I am?ls -l tools/clean.sh # 2. -rw-r--r-- <- no 'x' anywherechmod u+x tools/clean.sh # fix: owner gains execute -> -rwxr--r--
Key move:permission denied means the file was found β so this is not a path or lookup problem. chmod edits metadata; the scriptβs contents are irrelevant to the failure.
β οΈ Common Mistakes
π‘ > overwrites, | chains β redirect replaces the target file; pipe feeds the next program.
π‘ Mind the delimiter β cut -f assumes tab; for CSV use awk -F',' or cut -d','. Check the real delimiter first (/<tab> search in less).
π‘ wc/sort/uniq read the whole file β slow + memory-heavy on huge files; less/head only read the start β instant. Put cheap filters before them.
π‘ sort is alphabetical by default β add -n for numbers, and uniq only collapses adjacent duplicates (so sort | uniq).
π‘ > does not capture errors β it retargets stdout only; stderr still hits the screen and needs 2>.
π‘ Empty output β success β check echo $?; and a pipelineβs status is only its last stageβs.
π‘ permission denied β file missing β that is no such file or directory. Match the message to the stage before acting.