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:


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:

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:

THKI Insight

cat is 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:

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:

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:

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:

using tail -f.

Example:


tail -f /var/log/syslog

Press:


Ctrl+C

to stop monitoring.

THKI Insight

tail -f is 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:


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:

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:

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:

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:

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:

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

  1. Create a text file and display it using cat.
  2. Display two files with a single cat command.
  3. Open a large file using less and practice:
    • scrolling
    • searching
    • quitting
  4. Display the first five lines of a file using head.
  5. Display the last five lines using tail.
  6. Monitor a growing log file using:
  7. 
    tail -f logfile.txt
    

Stop monitoring with Ctrl+C.

  1. Use file to determine the type of:
  1. Count the number of lines in a file using:
  2. 
    wc -l filename
    
  3. Count the number of words using:
  4. 
    wc -w filename
    
  5. Combine commands using pipes:
  6. 
    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.