Viewing text files with cat, head, tail and less
Quick answer
You do not need an editor just to read a text file. Use cat for a short trusted file, head or tail for a bounded preview, and a pager for a long file. A pager shows one screen at a time without requiring you to change the file.
Prepare the practice environment
Use the chapter fixture described in lab-fixtures/text-streams/README.md. Run setup as a normal user in a disposable Linux environment, enter the printed directory and check pwd. Every example assumes a fresh fixture. Commands omit the terminal prompt; outputs below are not commands.
How it works
cat copies file contents to standard output; it does not add line numbers by default. head -n N and tail -n N choose the first and last N lines. With multiple files, GNU head and tail normally add headers. less and more are interactive pagers: Space moves forward and q quits; less also supports backward navigation and /pattern search. tac reverses line order, whereas nl numbers lines (by default, nonempty body lines). These commands do not make binary data safe to display. Use file first if unsure. tail -f follows growth until stopped with Ctrl+C; it is not needed for a static report.
Choose the tool by the question you need to answer: head shows the beginning, tail shows the end, and a pager lets you navigate without printing the entire file at once. A preview is not a check of every record. For example, an error in the middle of a log can be absent from both previews.
Try the examples
All three lines appear in file order.
Expected stdout:
startcheckfinishThe first and last line are selected without printing the middle.
Expected stdout:
startfinishIf less is installed, press Space to advance, /check then Enter to search, and q to exit. If absent, use more events.txt or the bounded preview above. This is a manual demonstration.
Manual interaction; no automated output asserted.
Common mistakes
Avoid cat on huge or unknown binary files: output can flood the terminal or contain control characters. Use a bounded text preview after identifying the file. Do not mistake tail -n 10 for following changes.
Practice
Print only the last two lines of events.txt, then explain which tool you would use to browse a 10,000-line text log. Expected last lines: check and finish.
Related references
Study: cat, head, tail, less, more, tac, nl. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Viewing text files — Knowledge Check
Apply the lesson concepts to the controlled text fixture.
Which tool prints only the last two lines?
How do you exit less?
What does tac reverse?
What does head -n 1 select?