Linux Mastery
The Human Knowledge Project
Chapter 10 — Viewing & Reading Files
Why This Chapter Matters
One of the most important Linux skills is learning how to inspect information quickly and safely.
Linux is a text-oriented operating system.
Configuration files...
Log files...
Scripts...
Source code...
System messages...
Command output...
Documentation...
Almost everything in Linux can be viewed, searched, filtered, and analyzed as text.
Learning to read information efficiently is one of the defining skills of an experienced Linux user.
Learning Objectives
Upon completing this chapter, you will be able to:
- display file contents with
cat - examine large files using
less - understand the differences between
lessandmore - inspect the beginning and end of files using
headandtail - determine file types with
file - count lines, words, and characters using
wc - combine viewing commands with pipes
- understand why Linux relies heavily on text
Introduction
Imagine being handed a book containing one million pages.
Suppose someone asks you to find a single error.
Would you read every page from beginning to end?
Of course not.
You would develop methods for examining only the information you need.
Linux works the same way.
Rather than opening every file in an editor, Linux provides specialized tools for viewing information quickly, safely, and efficiently.
These tools allow administrators to investigate problems, examine configuration files, inspect logs, and monitor systems without modifying the original data.
1. Reading Information in Linux
Linux communicates primarily through text.
Much of what the operating system knows can be viewed directly from the terminal.
Examples include:
- configuration files
- log files
- shell scripts
- source code
- command output
- documentation
Experienced Linux users spend far more time reading information than typing commands.
THKI Memory Aid
Look ↓ cat Browse ↓ less Peek at the beginning ↓ head Peek at the end ↓ tail Identify ↓ file Count ↓ wc
Remember this chart.
Each command has a different purpose.
Together they provide an efficient toolkit for inspecting information.
2. cat — Display File Contents
The cat command displays the contents of one or more files directly in the terminal.
Example:
cat notes.txt
Example output:
Buy SSD
Backup server
Update kernel
Why Is It Called cat?
The word cat is short for:
concatenate
Originally, the command was designed to join multiple files together.
Example:
cat file1.txt file2.txt
This displays the contents of both files one after the other.
Creating a File
echo "Hello Linux" > hello.txt
Display it:
cat hello.txt
Output:
Hello Linux
Viewing Multiple Files
cat one.txt two.txt three.txt
The files are displayed in the order listed on the command line.
When Not to Use cat
cat immediately displays the entire contents of a file.
For very large files this can overwhelm the terminal.
Example:
cat giant_log.txt
Thousands of lines may scroll past almost instantly.
Large files are usually better viewed with:
lessheadtail
THKI Insight
catis excellent for small files.It is usually the wrong tool for very large files.
3. less — Read Files Safely
The less command allows you to examine large files one screen at a time.
Example:
less logfile.txt
Unlike cat, less allows you to:
- scroll forward
- scroll backward
- search within a file
- move quickly through large documents
without displaying everything at once.
Basic less Controls
| Key | Action |
|------|--------|
| Space | Next page |
| b | Previous page |
| ↑ / ↓ | Scroll |
| /word | Search |
| n | Next match |
| q | Quit |
Why less Matters
System administrators commonly inspect files such as:
less /var/log/syslog
or
less /etc/fstab
because less allows careful examination without changing the file.
Searching Within less
To search for the word:
error
type:
/error
Press:
n
to move to the next matching occurrence.
4. more — The Original File Viewer
Before less became common, Unix systems used the more command.
Example:
more notes.txt
Like less, it pauses after each screen of output.
However, more provides fewer navigation features.
In modern Linux systems, less is generally preferred because it:
- scrolls backward
- searches more effectively
- handles large files more efficiently
Many Linux users jokingly say:
less is more
because less eventually became the better replacement for more.
5. head — View the Beginning of Files
The head command displays the beginning of a file.
Example:
head logfile.txt
By default, head displays the first ten lines.
Display a Specific Number of Lines
head -5 logfile.txt
This displays the first five lines.
Why head Is Useful
The beginning of a file often contains:
- headers
- startup messages
- configuration information
- file descriptions
head allows you to inspect this information quickly without opening the entire file.
6. tail — View the End of Files
The tail command displays the end of a file.
Example:
tail logfile.txt
By default, tail displays the last ten lines.
Display a Specific Number of Lines
tail -20 logfile.txt
This displays the last twenty lines.
Real-Time Monitoring
One of the most valuable Linux troubleshooting commands is:
tail -f logfile.txt
The option:
-f
means:
follow
Rather than exiting, tail continues watching the file and displays new lines as they are written.
System administrators commonly monitor:
- system logs
- web servers
- applications
- security events
- network services
using tail -f.
Example:
tail -f /var/log/syslog
Press:
Ctrl+C
to stop monitoring.
THKI Insight
tail -fis one of the most frequently used troubleshooting commands in Linux because it allows you to watch events as they happen.
7. file — Determine File Type
Unlike some operating systems, Linux does not rely entirely on filename extensions.
Instead, it examines the contents of a file to determine its actual type.
Example:
file notes.txt
Possible output:
ASCII text
Additional examples:
file picture.jpg
JPEG image data
file archive.zip
Zip archive data
Why This Matters
A filename can be misleading.
For example, someone could rename:
virus.exe
to:
vacation.jpg
The filename changes.
The contents do not.
The file command helps determine what a file actually contains.
It is widely used for:
- troubleshooting
- scripting
- security
- malware analysis
8. wc — Count Lines, Words, and Characters
The command:
wc
means:
word count
Example:
wc notes.txt
Possible output:
12 54 301 notes.txt
Meaning:
| Value | Represents |
|--------|------------|
| 12 | Lines |
| 54 | Words |
| 301 | Characters (or bytes) |
Common Options
| Command | Purpose |
|---------|---------|
| wc -l | Count lines |
| wc -w | Count words |
| wc -c | Count characters or bytes |
Examples:
wc -l logfile.txt
wc -w notes.txt
wc -c notes.txt
wc is especially useful for estimating file size and measuring text.
9. Pipes
One of the defining features of Linux is that commands can work together.
A pipe connects the output of one command to the input of another.
The pipe symbol is:
|
Example:
cat notes.txt | wc -l
Here:
catdisplays the file.wc -lcounts the lines.
Another example:
head logfile.txt | less
The first command selects the beginning of the file.
The second command allows you to browse the output one screen at a time.
Additional example:
tail logfile.txt | wc -l
Commands become much more powerful when combined together.
Linux Philosophy
Unix and Linux encourage programs to perform one job well.
Rather than creating one enormous program that performs every task, Linux provides many small tools that can be connected together.
Programs such as:
catlessheadtailfilewc
become remarkably powerful when combined using pipes.
This modular design is one of the defining characteristics of Unix and Linux.
10. Common Real-World Workflow
A Linux administrator investigating a problem might use the following sequence of commands:
Determine the file type:
file logfile.txt
Inspect the beginning of the file:
head logfile.txt
Inspect the most recent entries:
tail logfile.txt
Read the complete file safely:
less logfile.txt
Determine the size of the file:
wc -l logfile.txt
Together, these commands quickly reveal:
- file type
- startup information
- recent activity
- complete contents
- file length
Notice that each command performs one specific task.
Together they form a powerful troubleshooting toolkit.
11. Safety Note
The commands introduced in this chapter are primarily viewing commands.
They normally do not modify files.
Commands such as:
catlessmoreheadtailfilewc
are considered safe because they simply examine information.
Even so, always exercise caution when working with administrative privileges using sudo or when logged in as the root user.
Chapter Summary
| Command | Purpose |
|---------|---------|
| cat | Display the complete contents of a file |
| less | Browse large files safely |
| more | Older screen-by-screen file viewer |
| head | Display the beginning of a file |
| tail | Display the end of a file |
| file | Determine the true type of a file |
| wc | Count lines, words, and characters |
| Pipes (|) | Connect commands together |
Key Ideas
Linux communicates primarily through text.
Efficient Linux users learn to inspect information quickly without modifying it.
Understanding:
catlessheadtailfilewc- pipes
provides the foundation for troubleshooting, scripting, programming, and Linux system administration.
These commands demonstrate one of the central ideas of Unix and Linux:
Small tools, working together, solve complex problems.
Practice Exercises
- Create a text file and display it using
cat. - Display two files with a single
catcommand. - Open a large file using
lessand practice:- scrolling
- searching
- quitting
- Display the first five lines of a file using
head. - Display the last five lines using
tail. - Monitor a growing log file using:
tail -f logfile.txt
Stop monitoring with Ctrl+C.
- Use
fileto determine the type of:
- a text file
- an image
- a ZIP archive
- a shell script
- Count the number of lines in a file using:
- Count the number of words using:
- Combine commands using pipes:
wc -l filename
wc -w filename
cat filename | wc -l
head filename | less
tail filename | wc -l
Explain what each pipeline accomplishes.
Looking Ahead
Reading information is only the first step.
Soon we will begin creating and modifying files ourselves.
In the next chapter, we will learn how Linux text editors allow us to create, edit, and manage text efficiently from both the command line and graphical environments.