04 Viewing File Content
Learning objectives
- View a text file directly.
- Page through large files.
- View a file’s beginning, end, and live appended content.
Command overview
| Command | Purpose | Meaning | Basic usage | Example |
|---|---|---|---|---|
cat | Print an entire file | Short for concatenate; commonly prints a small file directly | cat file | cat README.md |
less | Page through a file | Suitable for browsing a large file, with scrolling and search | less file | less app.log |
head | View a file’s beginning | Shows the first 10 lines by default | head [options] file | head -n 20 app.log |
tail | View a file’s end | Shows the final 10 lines by default and can follow changes | tail [options] file | tail -f app.log |
wc | Count newlines, words, bytes, or characters | By default shows newline, word, and byte counts; -m counts characters | wc [options] file | wc -l app.log |
Common scenarios
View a small file:
cat config.txtPage through a large log:
less app.logView the beginning and end:
head -n 20 app.log
tail -n 50 app.logFollow newly appended log content:
tail -F -- app.logCount lines in a file:
wc -l app.logNotes for beginners
catis not suitable for opening a very large log directly because it can flood the terminal.- In
less, press/keywordto search,nfor the next match, andqto quit. tail -foccupies the terminal continuously; pressCtrl-cto stop it.- GNU
tail -Fretries by name and handles common log rotation. Options are not identical on every platform. wc -lcounts newline characters; the final text segment without a trailing newline does not increase that count.wc -ccounts bytes andwc -mcounts characters.- Logs can contain user input, tokens, internal addresses, and command lines. Do not publish an entire log directly.
Log-viewing exercise
On the fictional log from the integrated project, compare head -n 2, tail -n 2, wc -l, and less. State what range each command reads, whether it keeps running, and whether its output belongs in a report.