Read files without information overload
By the end: Choose a whole-file view, a preview, a pager, or a count to answer a specific question.
The idea
A good investigation asks for the smallest useful output. These file examples assume the lab below has created events.log in a fresh folder. For a huge log, start with its size and a small sample rather than printing everything.
Your command toolkit
cat
What it solves: Print or join small text files.
cat events.log
cat streams bytes to standard output. Avoid dumping binary files or very large logs into your terminal.
tac
What it solves: Read records in reverse line order.
tac events.log
Unlike reversing characters, tac reverses records. The last line appears first.
head
What it solves: Preview the beginning.
head -n 2 events.log
-n 2 selects the first two lines. It is useful for checking a header or recognizing an unfamiliar file.
tail
What it solves: Inspect the most recent lines.
tail -n 2 events.log
Use tail -f for a growing file and Ctrl+C to stop following. A static tail command exits on its own.
less
What it solves: Explore a file one screen at a time.
less events.log
Use /ERROR to search and q to quit. A pager lets you inspect context without flooding the terminal.
nl
What it solves: Add line numbers for discussion.
nl -ba events.log
-ba numbers blank lines too. Line numbers make a bug report much easier to reproduce.
wc
What it solves: Measure a text file.
wc -l events.log
-l counts newline characters. A final line without a newline changes the intuitive line count; -c counts bytes and -m counts characters.
fold
What it solves: Wrap long output for reading.
printf '%s\n' 'A long line is easier to review when wrapped.' | fold -sw 24
-s prefers breaking at spaces and -w sets the width. This prints a wrapped view; it does not rewrite a source file.
Before you move on
Try the guided lab below. Compare the result with the explanation, then record your reasoning in the workbook. A missing optional program or a restricted environment is something to explain, not a reason to change system settings blindly.
Practice with evidence
- Predict the result
- Run the guided lab
- Compare the output
- Explain your choice
A worked example
Guided lab
si_lab=$(mktemp -d)
cd "$si_lab" || exit
printf '%s\n' 'INFO start' 'INFO ready' 'ERROR timeout' 'INFO retry' > events.log
head -n 1 events.log
tail -n 2 events.log
wc -l events.log
What to expect: The first line is INFO start, the last two include the timeout and retry, and the newline count is 4.
Make it yours: Choose commands to inspect the first record, newest two records, and total line count of a large log. Explain why you would avoid cat for the full log.
Read further
- GNU Coreutils manual ↗www.gnu.org
Apply what you learned
Your private practice
Answers are private. Completion requires the correct knowledge-check answer and a written response when an activity is included.
Restore a downloaded workbook
Choose a recovery file for this course and version. Review the recovered copy before saving it to your account.
Ask a question or report a problem
Describe what needs attention. Your private answer is shared only if you select the option below.