<aside> 3️⃣

Module 3 — Reading and Editing Files

Nearly everything on a Linux server — configuration, logs, code, state — is plain text. This module teaches you to read it at any size, edit it in the two editors you will actually meet, understand what a file is underneath its name, and pack files for transport. Half of all real troubleshooting is done with the tools on this page.

Legend used throughout: 🧠 concept → 🧩 analogy → 🧪 exercise → ✅ expected result (hidden) → 🎯 interview questions (hidden)

</aside>

<aside> 🚦

Before you start

You need Module 1 — What Linux Is (commands, errors, man) and Module 2 — The Filesystem (paths, cd/ls, mkdir/cp/mv/rm, your home sandbox). Tools: the same Ubuntu 24.04 environment; nano, vim, tar, and gzip ship with it. This module's sandbox is ~/reading — create it now: mkdir ~/reading.

</aside>

<aside> ℹ️

This page uses Mermaid diagram blocks. Notion shows them as code by default — click the block and set it to Preview to see the diagram. This reminder appears once per page.

</aside>

Part A — Reading files at any size

Which tool reads what? One decision tree for the whole Part:

flowchart TD
    A["Need to read a file"] --> B{"Roughly how big?"}
    B -->|"a screenful"| C["cat<br>dump it all"]
    B -->|"big"| D{"Which part?"}
    D -->|"browse / search"| E["less<br>page both ways"]
    D -->|"just the start"| F["head"]
    D -->|"just the end"| G["tail"]
    D -->|"the end, as it grows"| H["tail -f<br>live follow"]
    A --> I{"Not sure it's text?"}
    I -->|"check first"| J["file<br>identify the type"]

A1. cat and file — dump it, but check first

<aside> 📖

Official docs: cat(1) · file(1)

</aside>

🧠 cat file prints a file's entire contents to the terminal, top to bottom, no pauses. (The name means concatenate — given several files it prints them joined, which is its original job.) Perfect for small files; wrong for big ones, and actively hazardous for non-text ones.

That second case is why file somefile exists: it inspects a file's content (not its name) and tells you what the content actually is — text, program, archive, image. Linux does not trust filename extensions; neither should you.

<aside> ⚠️

Trap — cat on a binary file garbles your terminal. Dumping program bytes to the screen can leave your prompt printing gibberish. Nothing is broken: type reset and press Enter (even if you cannot read what you are typing) and the terminal is reborn. Check unknown files with file before catting them.

</aside>

<aside> 🧩

Real-world analogy — reading the letter aloud, but sniff the envelope first

cat is an assistant who opens a letter and reads the whole thing aloud, start to finish, no pauses, no questions. file is the habit of checking the envelope first — this one's a letter, that one's a parcel, that one ticks.

Where the analogy stops working. A human reader adapts to the listener; cat reads at machine speed. Ask it to read a 2 GB log and it will, flooding your terminal for minutes — the tool has no judgement, which is exactly why the choice of tool is yours.

</aside>

🧪 Exercise A1.1 — cat the small, identify the strange

cat /etc/hostname          # a one-line file: this machine's name
cat /etc/os-release        # Module 2's friend — a proper small config file
file /etc/os-release       # what does content-inspection say it is?
file /usr/bin/man          # and a real program?
file /bin                  # and this one — surprise pending

A2. less — the pager for everything bigger

<aside> 📖

Official docs: less(1)

</aside>

🧠 less file opens a file in the same full-screen reader you met inside man in Module 1 — because man was using less all along. Same keys: Space page down, b page up, arrows scroll, /word search forward, n next match, N previous match, g jump to top, G jump to end, q quit. Nothing you view is ever modified — less is read-only by design, which makes it the safe default for looking at anything on a production machine.

<aside> 🧩

Real-world analogy — reading with your thumb in the book

less is proper book-reading: flip forward, flip back, scan for a phrase, jump to the index. cat was having the whole book read at you.

Where the analogy stops working. A book must exist in full before you read page one. less reads lazily — it opens a 10 GB file instantly because it loads only what you are looking at. This is why less on an enormous log is fine while cat on the same file is a mistake.

</aside>