Linux Basics 03 — Editing Text and Reading Files

Linux course index · Linux Basics category · Lesson 3 of 8

AI disclosure — AI-generated content, posted as is. AI can make mistakes. This lesson draws on the linked Open Education posts and adds original explanations or lab examples where useful. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.

Learning goal

Edit a text file in a terminal and verify the exact lines you saved.

Concepts and setup

The site’s vi example opens a new testfile. This version uses a short service checklist in a temporary lab. vi opens in normal mode. Press i to insert text, Esc to return to normal mode, and type :wq then Enter to save and exit. :q! exits without saving changes. If you are in the middle of a command, press Esc before trying again.

Use cat for short files, less for paging, and head or tail for selected ends. Avoid opening huge log files with cat. For live troubleshooting, tail -f follows a growing file and Ctrl-C stops following it.

Create and inspect a lab checklist

lab=$(mktemp -d)
cd "$lab" || exit
vi checklist.txt
# Inside vi, press i and type these three lines:
# check service
# inspect logs
# verify response
# Press Esc, then type :wq and press Enter.
nl -ba checklist.txt
head -n 1 checklist.txt
tail -n 1 checklist.txt

Possible output

     1  check service
     2  inspect logs
     3  verify response
check service
verify response

The comments describe key presses, not text to type into the file. If your lab has nano but not vi, use nano checklist.txt and save the same three lines.

Inspect with a pager

less checklist.txt
# Inside less, type /logs and press Enter.
# Press q to quit.

Possible output

check service
inspect logs
verify response

Your terminal may highlight the match and show an end-of-file marker. Typing q exits less without changing the file.

Questions only

Quick-check questions

  1. Why is i not used to save the file?
  2. What is the difference between :wq and :q!?

Assessment questions

  1. Which command prints only the last two lines?
  2. Change inspect logs to read logs and verify the edit using grep -n.
  3. Why prefer less over cat for a long report?

Questions and answers

Quick-check answers

  1. Why is i not used to save the file?
    Answer — i switches to Insert mode. Save commands are entered after returning to Normal mode.
  2. What is the difference between :wq and :q!?
    Answer — The first writes and exits. The second quits and discards unsaved edits.

Assessment answer key

  1. Which command prints only the last two lines?
    Answer — tail -n 2 checklist.txt.
  2. Change inspect logs to read logs and verify the edit using grep -n.
    Answer — In vi, search with /inspect logs, then edit the line, save, and run grep -n ‘read logs’ checklist.txt. The expected line is 2:read logs.
  3. Why prefer less over cat for a long report?
    Answer — less pages and searches without dumping the entire report into the terminal.

Source articles on this site

Continue the course

Previous lesson 02 — Paths, Files, and Links. Next lesson 04 — Pipes, Redirection, and Exit Status.

Linux course index · Linux Basics category · Lesson 3 of 8

Leave a Reply