Bash for Cybersecurity
A log's ERROR record is data, not necessarily the search tool's diagnostic. Writing to stderr does not itself establish a failure status.
file app.log head -n 2 app.log tail -n 2 app.log
The fixture has five records: three ERROR, one WARNING, and one INFO. Use less app.log in a terminal; /ERROR, n, and q inspect and exit. Do not start indefinite followers in notebook cells.
less app.log
/ERROR
n
q
grep -F '|ERROR|' app.log grep -nF '|WARNING|' app.log grep -cF '|ERROR|' app.log
-F treats the pattern as fixed text; severity separators matter. A count of selected lines is not a count of every pattern occurrence. No match and an unreadable input are different status/diagnostic observations.
-F
wc -l app.log wc -c app.log
-l counts newline characters; -c counts bytes. Unterminated text can contain bytes but zero newlines. Use od -An -tx1 sample.bin for a precise byte view of the four-byte fixture.
-l
-c
od -An -tx1 sample.bin
grep -F '|ERROR|' app.log | wc -l grep -F '|ERROR|' app.log > matches.txt wc -l < matches.txt cat matches.txt
The count is three. Check the selected records as well as the number. An ordinary pipe carries stdout, not stderr.
printf '%s\n' 'Review started' > notes.txt printf '%s\n' 'Source is synthetic' >> notes.txt cat notes.txt
> creates or truncates; >> appends. Never redirect a transformation over the file it is reading. Keep an independent source copy for comparison.
>
>>
LC_ALL=C sort app.log | grep -F '|ERROR|' > sorted_first.txt grep -F '|ERROR|' app.log | LC_ALL=C sort > filtered_first.txt diff sorted_first.txt filtered_first.txt
These stages commute for this fixture; arbitrary commands do not. Filtering first can reduce the number of records sort receives.
grep -F '|ERROR|' app.log | cut -d '|' -f 3 | LC_ALL=C sort | uniq
Three ERROR records contain two distinct message texts. uniq collapses adjacent duplicates; sort brings them together. This supplied extension precedes the fuller field-tool treatment in Chapter 11.
uniq
grep -F '|ERROR|' app.log > selected.txt 2> search_errors.txt wc -l < selected.txt wc -c < search_errors.txt diff app.log source_reference.txt
Expected: three selected lines, zero diagnostic bytes, unchanged source. A count alone does not establish that every pipeline stage succeeded.
Linuxize: streams, head, tail, grep, wc, and redirection.
Chapter 10 develops descriptor/pipeline failure mechanics; Chapter 11 develops transformations; Chapter 12 develops here-documents.