Skip to content

Viewing short and long files

Module
Reading and Editing Text
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21; Ubuntu 24.04.4 LTS (course VM)
On this page

A log fills your terminal. Its last lines are visible, but the beginning has scrolled away. Printing it again does not help you find a particular entry: it sends the same long stream past you.

By the end, you can choose a tool for a short note, inspect either end of a log, and search a long file without changing it.

Finish Practice: Working with Files and Directories (B06). Keep your existing ~/lab from B04.1’s setup. This module uses only ~/lab/b07; earlier notes and exercises remain outside it.

Build this lesson’s disposable files once. If you have already started, use Reset this lesson first.

Terminal window
mkdir -p ~/lab/b07/view
cd ~/lab/b07/view
printf 'Read the log\nFind the checkpoint\n' > short.txt
printf 'Line %02d: routine check\n' {1..39} > long.log
printf 'Line 40: CHECKPOINT reached\n' >> long.log
printf 'Line %02d: routine check\n' {41..80} >> long.log

The first printf writes a two-line note. The other three build an 80-line log with a checkpoint on line 40. Copy the numbered setup as written: the braces generate a range of numbers, which B08 explains. B04’s setup used > to write a file. >> is introduced here: it adds to the file’s end. B09 teaches those operators in detail.

Every remaining command starts in ~/lab/b07/view.

Terminal window
cat short.txt
Captured output
Read the log
Find the checkpoint

cat prints the complete contents and returns to Bash. It suits a note short enough to read on one screen. It does not open an editor, and it does not need a quit key when given this filename.

Print the complete short note
learner@rootvolt:~/lab/b07/view$ cat short.txtRead the log
Find the checkpointlearner@rootvolt:~/lab/b07/view$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. cat prints both lines of the short note and returns to the prompt.
Steps (3)
  1. Type cat short.txt
  2. Press Enter
  3. Output: Read the log Find the checkpoint

The static transcript contains the same two lines. On a long file, the complete output can be inconvenient even though the command succeeds. Use a pager to read at your own pace, or ask for a smaller part.

Predict first: do these commands change the log, or choose which lines to display?

Terminal window
head -n 2 long.log
tail -n 2 long.log
Run it, then compare with your guess
head output
Line 01: routine check
Line 02: routine check
tail output
Line 79: routine check
Line 80: routine check

They only read. head selects the beginning; tail selects the end. Neither edits or removes the lines it leaves out.

-n takes a number of lines, as in B04.1. Without it, both programs print ten lines by default. A line here ends with a newline character, the invisible separator introduced in B04 practice; B07.5 examines its stored form.

Choose two lines from each end
learner@rootvolt:~/lab/b07/view$ head -n 2 long.logLine 01: routine check
Line 02: routine checklearner@rootvolt:~/lab/b07/view$ tail -n 2 long.logLine 79: routine check
Line 80: routine checklearner@rootvolt:~/lab/b07/view$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. head selects the first two lines; tail selects the last two.
Steps (6)
  1. Type head -n 2 long.log
  2. Press Enter
  3. Output: Line 01: routine check Line 02: routine check
  4. Type tail -n 2 long.log
  5. Press Enter
  6. Output: Line 79: routine check Line 80: routine check

The replay’s first result is lines 01–02; its second is lines 79–80. Those are two views of one file, not two different copies.

A pager displays text a screen at a time. You met less as the pager used by man in B04.3. You can also give it a filename directly:

Terminal window
less -N long.log

-N displays a line number beside each line. These numbers are not written into long.log. The terminal is occupied by less; do not type shell commands into it.

less shows the start of long.log, numbered 1 to 29, with long.log on its bottom row.
less opens long.log with line numbers. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. Line numbers belong to the pager display, not the saved file.
  2. The bottom row names the file; Bash is not waiting for a command.

Use these keys while the pager is open:

Key Action
Space Move forward one screen
b Move backward one screen
Up / Down Move one line
g / G Go to the beginning / end
/word, Enter Search forward for word
n / N Find the next / previous match
q Quit to the shell

Press Space once and b once. The first key advances through the text; the second returns toward its beginning. The file remains unchanged.

Type /CHECKPOINT, then press Enter. Look at line 40:

less displays line 40 with CHECKPOINT highlighted; later routine lines follow.
A forward search reaches the checkpoint. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. CHECKPOINT is highlighted on line 40 after the forward search.
  2. The pager remains open; q returns to Bash.

The slash begins a search, and Enter runs that search inside the pager. The match is highlighted in this captured less 590 session. This is a different input context from Bash: Enter has not launched a shell command.

Press q, then check the file from the prompt:

Terminal window
wc -l long.log
head -n 2 long.log
tail -n 2 long.log
The shell shows wc, head and tail results and a prompt in the view directory.
After q, Bash reads the unchanged file. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. The saved log still has 80 lines, with the same first and last two lines.
  2. The Bash prompt is back after q.

The count is still 80. The first two lines still begin with 01 and 02; the last two still begin with 79 and 80. Reading and searching did not edit the file.

Omitting a file that exists. Check the named path when a read fails:

Terminal window
cat missing.log
Captured output
cat: missing.log: No such file or directory

The program ran, but the path does not exist here. Use ls or Tab to check the name. Do not change permissions or add sudo to correct a spelling mistake.

Omitting the number after -n.

Terminal window
head -n long.log
Captured output
head: invalid number of lines: ‘long.log’

As in B04.1, the next word became the option’s value. Put a number between -n and the filename.

Typing a shell command inside less. Keys go to the pager until you quit it. Press q and wait for the Bash prompt before running cat or another command.

Goal: Read the instruction at each end of the short note, then locate the checkpoint without printing the entire long log.

  1. Print only the last line of short.txt.
  2. Print only its first line.
  3. Open long.log with line numbers, find CHECKPOINT, and return to Bash.

Expected: Two different note lines, checkpoint line 40, and a returned shell prompt.

Hint

The number belongs to -n. The pager search begins with a slash, not a shell command.

Solution
Terminal window
tail -n 1 short.txt
Captured output
Find the checkpoint
Terminal window
head -n 1 short.txt
Captured output
Read the log
Terminal window
less -N long.log

Type /CHECKPOINT, Enter, then q. Line 40 contains the checkpoint, as in the screenshot above.

Return to the parent and inspect this lesson’s directory:

Terminal window
cd ~/lab/b07
ls -AR view

The directory contains only long.log and short.txt after the exercise.

Terminal window
rm -r ~/lab/b07/view

Rerun the setup under Before you start. If view is already absent, skip removal. The setup restores the same note and log.

  • cat prints an entire file; it suits a short note.
  • head and tail select the beginning and end; -n chooses the line count.
  • less -N adds display line numbers and lets you page and search without editing.
  • Keys go to the pager until q returns you to Bash.

Next, Following a changing file distinguishes a finished read from an active watcher.