Viewing short and long files
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.
Before you start
Section titled “Before you start”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.
mkdir -p ~/lab/b07/viewcd ~/lab/b07/viewprintf 'Read the log\nFind the checkpoint\n' > short.txtprintf 'Line %02d: routine check\n' {1..39} > long.logprintf 'Line 40: CHECKPOINT reached\n' >> long.logprintf 'Line %02d: routine check\n' {41..80} >> long.logThe 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.
Print a short file
Section titled “Print a short file”cat short.txtRead the logFind the checkpointcat 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.
learner@rootvolt:~/lab/b07/view$ cat short.txtRead the log
Find the checkpointlearner@rootvolt:~/lab/b07/view$ Steps (3)
- Type
cat short.txt - Press Enter
- 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.
Choose the beginning or the end
Section titled “Choose the beginning or the end”Predict first: do these commands change the log, or choose which lines to display?
head -n 2 long.logtail -n 2 long.logRun it, then compare with your guess
Line 01: routine checkLine 02: routine checkLine 79: routine checkLine 80: routine checkThey 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.
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$ Steps (6)
- Type
head -n 2 long.log - Press Enter
- Output:
Line 01: routine check Line 02: routine check - Type
tail -n 2 long.log - Press Enter
- 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.
Keep a long file on screen
Section titled “Keep a long file on screen”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:
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.

- Line numbers belong to the pager display, not the saved file.
- 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.
Find the checkpoint
Section titled “Find the checkpoint”Type /CHECKPOINT, then press Enter. Look at line 40:

- CHECKPOINT is highlighted on line 40 after the forward search.
- 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:
wc -l long.loghead -n 2 long.logtail -n 2 long.log
- The saved log still has 80 lines, with the same first and last two lines.
- 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.
Mistakes to recognize
Section titled “Mistakes to recognize”Omitting a file that exists. Check the named path when a read fails:
cat missing.logcat: missing.log: No such file or directoryThe 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.
head -n long.loghead: 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.
Try it yourself
Section titled “Try it yourself”Goal: Read the instruction at each end of the short note, then locate the checkpoint without printing the entire long log.
- Print only the last line of
short.txt. - Print only its first line.
- Open
long.logwith line numbers, findCHECKPOINT, 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
tail -n 1 short.txtFind the checkpointhead -n 1 short.txtRead the logless -N long.logType /CHECKPOINT, Enter, then q. Line 40 contains the checkpoint, as in the screenshot above.
Reset this lesson
Section titled “Reset this lesson”Return to the parent and inspect this lesson’s directory:
cd ~/lab/b07ls -AR viewThe directory contains only long.log and short.txt after the exercise.
rm -r ~/lab/b07/viewRerun the setup under Before you start. If view is already absent, skip removal. The setup restores the same note and log.
catprints an entire file; it suits a short note.headandtailselect the beginning and end;-nchooses the line count.less -Nadds display line numbers and lets you page and search without editing.- Keys go to the pager until
qreturns you to Bash.
Next, Following a changing file distinguishes a finished read from an active watcher.
Further reading
Section titled “Further reading”- On your system:
man cat,man head,man tail, andman less. - GNU Coreutils: head invocation and tail invocation.
- less documentation, including the manual for paging, searches, and
-N.