Table of Contents
Why “Viewing” Tools Matter
Before editing, you usually want to look at a file: inspect logs, read configs, skim scripts, or copy a snippet. Viewing tools are optimized for this: quick look, search, scroll, maybe copy, but not full editing.
This chapter focuses on common command‑line tools for viewing text:
cat,tacless,morehead,tailnlwcstrings
You’ll see what each is good at and a few essential options/keystrokes.
Simple Viewers: `cat` and `tac`
`cat` — dump file contents
cat (“concatenate”) prints file contents to standard output:
cat filenameTypical uses:
- Quickly show a short file:
cat /etc/hostname- Combine files:
cat part1.txt part2.txt > combined.txtUseful options for viewing:
- Show line numbers:
cat -n file.txt- Show non‑printing characters (tabs/newlines), handy for debugging whitespace:
cat -A file.txt-Ais equivalent to-vET(show nonprintable,^Mfor CR, etc.)
cat is ideal for small files. For large files, it will scroll off your screen too fast; use less instead.
`tac` — `cat` backwards
tac prints lines in reverse order:
tac file.txt
Typical use: quickly see the end of a file that isn’t constantly changing (for active logs, tail is usually better):
tac big.log | head
This shows the last few lines, like tail, but by reversing first then taking the top 10.
Paging Through Files: `less` and `more`
When a file is longer than the screen, you want a pager: a program that lets you scroll, search, and quit when done.
`less` — the standard pager
less is the de‑facto standard text viewer on Linux. Basic usage:
less filenameKey things to know:
- You do not edit the file; you only view it.
- It loads files lazily, so it’s good even for very large files.
Essential key commands in `less`
Inside less, use these keys (no : needed):
Movement:
↑/k— one line up↓/j— one line downSpace— one page (screen) downb— one page upg— go to start of fileG— go to end of file
Searching:
/pattern— search forward forpattern- Example:
/errorthen pressEnter ?pattern— search backward forpatternn— repeat last search in same directionN— repeat last search in opposite direction
Other useful keys:
q— quit=— show current file name and line infoh— help screen (shows all commands)
Viewing multiple files with `less`
You can open several files at once:
less file1.txt file2.txt
While inside less:
:n— go to next file:p— go to previous file
Using `less` after another command
less is often used in pipelines to page command output:
dmesg | less
ps aux | lessThis is crucial when output is longer than your terminal.
`more` — older, simpler pager
more is an older pager with fewer features than less:
more filenameBasic keys:
Space— next pageEnter— next lineq— quit/pattern— search forward
In modern Linux systems, less is preferred; you’ll mostly see more in older scripts or documentation.
Viewing the Start and End of Files: `head` and `tail`
Often you only care about the beginning or end of a file.
`head` — view the beginning of a file
By default, head shows the first 10 lines:
head file.txt
Change the number of lines with -n:
head -n 20 file.txt # first 20 lines
head -5 file.txt # shorthand for -n 5 (in many shells)Useful with logs or configs to get a quick sense of what’s inside without opening a pager.
You can also use it in pipelines:
ls -l | head`tail` — view the end of a file
By default, tail shows the last 10 lines:
tail file.txt
tail -n 50 /var/log/syslog # last 50 linesFollowing a file in real time
One of the most useful features:
tail -f /var/log/syslog
-f (“follow”) keeps tail running and prints new lines as the file grows. This is great for monitoring logs while something is happening.
To stop, press Ctrl+C.
You can combine options:
tail -n 50 -f /var/log/syslogThis shows the last 50 lines, then continues showing new lines as they are written.
Line Numbers and Counts: `nl` and `wc`
`nl` — show line numbers
nl (“number lines”) prints a file with numbered lines:
nl file.txtThis is convenient when:
- Discussing a specific line in a config file
- Debugging a script where error messages refer to line numbers
Options you’ll commonly use:
- Use a specific width for numbers (e.g. 3 digits):
nl -w3 file.txt- Number all lines, including blank ones:
nl -ba file.txt`wc` — count lines, words, and bytes
wc (“word count”) summarizes text:
wc file.txtOutput format:
$lines $words $bytes filename
Common options:
- Lines only:
wc -l file.txt- Words only:
wc -w file.txt- Characters (not bytes):
wc -m file.txt
You can use wc with pipelines to count lines of command output:
ls | wc -l # how many entries in this directory?
grep "ERROR" app.log | wc -l # how many lines match "ERROR"?Extracting Printable Text: `strings`
Some files are not plain text (binaries, compiled programs, data files), but may contain embedded text messages. strings finds printable character sequences in these files:
strings /bin/lsUseful for:
- Quickly checking if a file contains readable text
- Inspecting binaries for messages, paths, or hints
You can also pipe to grep to search:
strings somefile | grep "password"Be careful: this does not magically decrypt or decode data; it just shows sequences of printable characters.
Choosing the Right Tool
Typical situations and recommended tools:
- Quick look at a short text file:
cat,head,tail - Read a long file comfortably:
less - Monitor a log as it changes:
tail -f logfile - See first/last N lines:
head -n N,tail -n N - Need line numbers while reading:
nl fileorless+=for position info - Count lines/words in output:
wcorwc -lin a pipeline - Glance at text inside a binary:
strings
Practice combining these with pipes (|) to create powerful “viewing pipelines” without ever leaving the command line.