Free Ebook cover Linux Command Line for Beginners: Navigate, Search, and Automate Simple Tasks

Linux Command Line for Beginners: Navigate, Search, and Automate Simple Tasks

New course

10 pages

Viewing and Inspecting File Contents from the Terminal

Capítulo 4

Estimated reading time: 5 minutes

+ Exercise

Why inspect files from the terminal?

Many tasks on Linux involve quickly checking what’s inside a file: confirming a configuration value, scanning a log, or counting how many lines a report contains. Terminal tools let you do this fast, even over SSH, without opening a graphical editor.

cat: print small files quickly

cat outputs a file’s contents to the terminal. It’s best for small files because it prints everything at once.

Basic usage

cat notes.txt

Show line numbers (useful for referencing)

cat -n notes.txt

Combine files into one stream

You can view multiple files in sequence:

cat part1.txt part2.txt

Because cat writes to standard output, you can also feed its output into other commands later (for example, counting lines with wc).

less: page through files safely (search, jump, quit)

less is a pager: it lets you view large files one screen at a time without dumping everything to the terminal. It’s a default choice for logs and long text files.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

Open a file in less

less /var/log/syslog

Essential navigation keys inside less

  • Move down: Space (page), Enter (line), or arrow keys
  • Move up: b (page up) or arrow keys
  • Quit: q

Search within less

Search forward for a word:

/error

Then jump between matches:

  • Next match: n
  • Previous match: N

Jump to a location

  • Go to start of file: g
  • Go to end of file: G
  • Go to a specific line number: type the number then g (example: 120g)

Tip: open at the end (handy for logs)

less +G /var/log/syslog

This opens the file positioned at the end, so you can immediately see the most recent entries.

head and tail: preview the beginning or end

head shows the first lines of a file; tail shows the last lines. These are perfect for quick inspection without opening a pager.

Default behavior (first/last 10 lines)

head report.txt
tail report.txt

Choose how many lines with -n

head -n 20 report.txt
tail -n 20 report.txt

Follow a growing file with tail -f

tail -f keeps running and prints new lines as they are appended to the file (common for live log monitoring).

tail -f /var/log/syslog

Stop following with Ctrl+C.

Follow and start with the last N lines

This is a common pattern: show the last 50 lines, then keep following.

tail -n 50 -f /var/log/syslog

wc: count lines, words, and bytes

wc (word count) summarizes file size in terms of lines, words, and bytes. It’s useful for quick metrics and sanity checks.

Count lines, words, and bytes

wc notes.txt

The output is typically: lines words bytes filename.

Count only lines

wc -l notes.txt

Count only words

wc -w notes.txt

Count only bytes

wc -c notes.txt

Count multiple files at once

wc -l chapter1.txt chapter2.txt

wc will also print a total line when given multiple files.

file: detect what kind of file you’re looking at

File extensions are not always reliable. The file command inspects a file’s contents and reports its type (text, image, compressed archive, executable, etc.). This helps you choose the right tool to open or process it.

Identify a file type

file document.txt
file archive.tar.gz
file /bin/ls

Common results you might see

  • ASCII text or UTF-8 Unicode text for plain text files
  • gzip compressed data for gzipped files
  • ELF 64-bit LSB executable for Linux binaries

Mini-lab: inspect a log file, follow new entries, and summarize counts

This mini-lab practices quick inspection patterns you’ll use constantly. Use a log file that exists on your system. Common choices include /var/log/syslog (Debian/Ubuntu) or /var/log/messages (some other distributions). If one doesn’t exist, try another.

1) Pick a log file and confirm its type

file /var/log/syslog

If that path doesn’t exist, try:

file /var/log/messages

2) Preview the log quickly

Look at the beginning and end to understand the format and recency.

head -n 15 /var/log/syslog
tail -n 15 /var/log/syslog

3) Page through the log and search for a keyword

Open in less, then search for something common like error, fail, or a service name.

less /var/log/syslog
  • Inside less, type /error then press Enter
  • Press n to jump to the next match
  • Press G to jump to the end
  • Press q to quit

4) Follow new log entries live

In one terminal, start following the log:

tail -n 20 -f /var/log/syslog

Leave it running for a moment. If the system is quiet, you may not see new lines immediately. When you’re done, stop with Ctrl+C.

5) Summarize line and word counts for selected files

Choose a few text files you want to measure (for example, a couple of logs or text documents). Then run wc to compare them.

wc -l /var/log/syslog /var/log/auth.log
wc -w /var/log/syslog /var/log/auth.log

If /var/log/auth.log doesn’t exist on your system, substitute another file you have access to (for example, /var/log/messages).

6) Quick check: use cat only when it’s small

Pick a small config or note file and view it with cat. If the output scrolls too fast, switch to less next time.

cat smallfile.txt

Now answer the exercise about the content:

You want to monitor a log file so you can see the last 50 lines immediately and then keep displaying new lines as they are added. Which command best fits this task?

You are right! Congratulations, now go to the next page

You missed! Try again.

tail -n 50 -f shows the last 50 lines and then follows the file, printing new lines as they are appended.

Next chapter

Permissions and Ownership: Read, Write, Execute in Practice

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.