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, andless - Show the beginning or end of a file with
headandtail - 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#
- Show the first 10 lines of
sample.log:_____ sample.log(default) orhead -n 10 sample.log - Show the last 20 lines:
tail ___ 20 sample.logortail -20 sample.log - Count lines in
notes.txt:wc ___ notes.txt - Compare
a.txtandb.txt:_____ a.txt b.txt - Page through a long file:
_____ sample.log
Answers
head-n-ldiffless(ormore)
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:
head -5 sample.logandtail -5 sample.log— confirm the entry numbers make sense.- Open
less sample.log, jump to the end withG, quit withq. - Run
diff a.txt b.txtand explain the change in one sentence in your notes. - Run
wc -l sample.logand 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:
- Create
story.txtwith at least 15 lines (printfin a loop, or a here-document). - Practice
cat,head -n 5,tail -n 5, andlesson it. - Duplicate the file to
story-v2.txt, change one middle line with any method you know (sedoptional; or recreate the file). diff story.txt story-v2.txtand save the output intostory.diffusing 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#
caton multi-megabyte files and losing earlier output off the scrollback- Editing production configs in place before taking a copy — view and
difffirst - Misreading
diffdirection (<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.