← Linux from ScratchCheat sheet
M4Linux from Scratch

M4 — Viewing and comparing file content

Module brief#

Read file contents without opening a full editor: whole files, paged views, tops and tails, word counts, and simple comparisons with diff.


Learning objectives#

  • Display files with cat, more, and less
  • Show the beginning or end of a file with head and tail
  • Count lines, words, and bytes with wc
  • Compare two text files with diff
  • Choose a viewer based on file length

1. cat — concatenate and print#

cat writes file contents to the terminal. Great for short files; noisy for huge logs.

cd /tmp/linux-lab-pushpjeet
cat notes.txt
Line one
Line two
Line three
Line four
Line five
cat readme.txt
Hello Linux learners
This is a practice file.
Keep exploring the CLI.

You can cat several files in order; they print one after another.


2. Pagers: more and less#

For long output, use a pager. less is usually preferred (scroll both ways). more is an older pager that mainly scrolls forward.

less sample.log
# navigate: Space / b / g / G / /pattern / q
more sample.log

Inside less:

Key Action
Space or f Forward
b Back
g Top of file
G End of file
/word Search
q Quit

Tip: man itself uses a pager — you already practised those keys in Module 2.

If a command floods the screen, pipe into less (pipes appear fully in Module 9):

ls -l /usr/bin | less

3. head and tail#

head -3 sample.log
log entry 1 at 11:04:57
log entry 2 at 11:04:57
log entry 3 at 11:04:57
tail -5 sample.log
log entry 36 at 11:04:57
log entry 37 at 11:04:57
log entry 38 at 11:04:57
log entry 39 at 11:04:57
log entry 40 at 11:04:57

Defaults are often 10 lines when you omit -n / -NUMBER.

Administrators watch growing logs with tail -f logfile (follow mode). Stop with Ctrl+C. Use it on logs you own in a lab — not on production systems without permission.


4. wc — word count (and more)#

wc notes.txt sample.log
   5   10   49 notes.txt
  40  200  991 sample.log
  45  210 1040 total

Columns are typically: lines, words, bytes, then the filename.

Useful flags: wc -l (lines only), wc -w (words), wc -c (bytes).


5. diff — compare two files#

cat a.txt
alpha
beta
gamma
cat b.txt
alpha
beta
delta
diff a.txt b.txt
3c3
< gamma
---
> delta

How to read this style of diff:

  • 3c3 — change line 3 on the left file to line 3 on the right
  • < lines are from the first file
  • > lines are from the second file

No output from diff usually means the files are identical. Exit status is 0 when same, 1 when different (handy later in scripts).


6. Choosing a tool quickly#

Situation Reach for
Tiny file cat
Long file you will skim less
First few lines head
Last few lines / recent log lines tail
How big is this text? wc -l
What changed between two configs? diff

Editors like vim arrive in Module 7. Viewing comes first so you inspect safely before editing.


Practice: MCQs#

Q1. Best default tool for browsing a 5,000-line log interactively?

  • A) cat
  • B) less
  • C) touch
  • D) mkdir
Answer

B. less pages and searches. cat dumps everything at once.

Q2. head -n 20 file shows:

  • A) The last 20 lines
  • B) The first 20 lines
  • C) Twenty random lines
  • D) Lines containing the number 20
Answer

B. head reads from the start; tail from the end.

Q3. In diff output, a line starting with < means:

  • A) It comes from the first file argument
  • B) It comes from the second file argument
  • C) The line is a comment
  • D) The line should be deleted from disk immediately
Answer

A. < is first file; > is second file.

Q4. wc -l notes.txt is best when you need:

  • A) A full encrypted backup
  • B) Only the line count
  • C) A binary disassembly
  • D) Permission bits
Answer

B. -l limits output to lines.

Q5. tail -f is commonly used to:

  • A) Format disks
  • B) Follow a log file as new lines appear
  • C) Find hard links
  • D) Compile C programs
Answer

B. Follow mode streams new content until you interrupt it.


Practice: Fill in the blank#

  1. Show the first 10 lines of sample.log: _____ sample.log (default) or head -n 10 sample.log
  2. Show the last 20 lines: tail ___ 20 sample.log or tail -20 sample.log
  3. Count lines in notes.txt: wc ___ notes.txt
  4. Compare a.txt and b.txt: _____ a.txt b.txt
  5. Page through a long file: _____ sample.log
Answers
  1. head
  2. -n
  3. -l
  4. diff
  5. less (or more)

Practice: Mini terminal challenge#

Challenge: Inspect and compare#

Goal: Use head, tail, less, and diff on real files.

Setup: Use /tmp/linux-lab-pushpjeet with sample.log, a.txt, and b.txt (recreate them if needed).

Tasks:

  1. head -5 sample.log and tail -5 sample.log — confirm the entry numbers make sense.
  2. Open less sample.log, jump to the end with G, quit with q.
  3. Run diff a.txt b.txt and explain the change in one sentence in your notes.
  4. Run wc -l sample.log and confirm it matches how many lines you expect.

Verify: Your notes include the diff summary (gamma vs delta) and a line count.

Stretch: Create c.txt identical to a.txt and show that diff a.txt c.txt prints nothing.


Hands-on lab#

Goal: Comfortable file inspection without an editor.

Steps:

  1. Create story.txt with at least 15 lines (printf in a loop, or a here-document).
  2. Practice cat, head -n 5, tail -n 5, and less on it.
  3. Duplicate the file to story-v2.txt, change one middle line with any method you know (sed optional; or recreate the file).
  4. diff story.txt story-v2.txt and save the output into story.diff using redirection if you already know > (or copy by hand).

Verify: wc -l story.txt reports at least 15; diff shows your intentional change.

Stretch: Compare two small configuration snippets from /etc that you are allowed to read (for example two timezone-related files on some systems) — or stick to files you created.


Common mistakes#

  • cat on multi-megabyte files and losing earlier output off the scrollback
  • Editing production configs in place before taking a copy — view and diff first
  • Misreading diff direction (< vs >)
  • Forgetting to quit less (q) and wondering why the shell ignores new commands

Summary#

You can inspect text safely: cat for short files, less for long ones, head/tail for edges, wc for counts, and diff for changes. Next you will create and reorganise files yourself.

Continue

← Linux from Scratch hub · Cheat sheet · All tutorials