Chapter 4: Files, Streams, and Pipes

Bash for Cybersecurity

Learning objectives

  1. Explain how the Unix data model treats files and streams.
  2. Inspect text and binary data with standard command-line tools.
  3. Inspect and count matching log records with literal grep patterns.
  4. Trace input, output, and diagnostics through a basic pipeline.
  5. Save and verify a result without overwriting its source.

Chapter flow

  1. Everything Is a File
  2. Inspecting Text
  3. Pipes and Redirection

Common interfaces, different resources

  • A regular file stores bytes that can be revisited by pathname.
  • A pipe carries a stream between processes.
  • Devices and sockets can share read/write interfaces without behaving as regular files.
  • Text tools require an appropriate data format; not all bytes are text.

Standard channels

Descriptor Channel Purpose
0 stdin Input.
1 stdout Normal results.
2 stderr Diagnostics.

A log's ERROR record is data, not necessarily the search tool's diagnostic.
Writing to stderr does not itself establish a failure status.

Inspect a known synthetic input

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.

Literal field selection

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.

Count the intended thing

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.

Connect stages and inspect their results

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.

Save without overwriting the source

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.

Compare operation order

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.

Extension: events and distinct messages

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.

Separate results from diagnostics

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.

Sources and later boundaries

Linuxize: streams,
head,
tail,
grep,
wc, and
redirection.

Chapter 10 develops descriptor/pipeline failure mechanics;
Chapter 11 develops transformations; Chapter 12 develops here-documents.