Kahibaro
Discord Login Register

2.3.1 Viewing tools

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:

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 filename

Typical uses:

  cat /etc/hostname
  cat part1.txt part2.txt > combined.txt

Useful options for viewing:

  cat -n file.txt
  cat -A file.txt

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 filename

Key things to know:

Essential key commands in `less`

Inside less, use these keys (no : needed):

Movement:

Searching:

Other useful keys:

Viewing multiple files with `less`

You can open several files at once:

less file1.txt file2.txt

While inside less:

Using `less` after another command

less is often used in pipelines to page command output:

dmesg | less
ps aux | less

This is crucial when output is longer than your terminal.

`more` — older, simpler pager

more is an older pager with fewer features than less:

more filename

Basic keys:

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 lines

Following 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/syslog

This 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.txt

This is convenient when:

Options you’ll commonly use:

  nl -w3 file.txt
  nl -ba file.txt

`wc` — count lines, words, and bytes

wc (“word count”) summarizes text:

wc file.txt

Output format:

Common options:

  wc -l file.txt
  wc -w file.txt
  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/ls

Useful for:

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:

Practice combining these with pipes (|) to create powerful “viewing pipelines” without ever leaving the command line.

Views: 73

Comments

Please login to add a comment.

Don't have an account? Register now!