Viewing and processing text files

3.2 Searching and Extracting Data from Files (Weight: 3)

📘Linux Essentials (LPI 010-160)


Viewing and Processing Text Files

In Linux systems, most configuration files, logs, scripts, and data files are stored as plain text files. Because of this, Linux administrators must know how to view, navigate, and process text files efficiently from the command line.

For the Linux Essentials (LPI 010-160) exam, understanding how to display file content, browse large files, and process text output is an important skill. These commands allow administrators to inspect configuration files, analyze logs, and extract important information.

This section explains the essential tools used to view and process text files in Linux.


1. What is a Text File in Linux?

A text file is a file that contains readable characters instead of binary data.

Examples of common Linux text files:

  • /etc/passwd → user account information
  • /etc/hosts → hostname mappings
  • /etc/fstab → filesystem mount configuration
  • /var/log/syslog or /var/log/messages → system logs

These files can be read and processed directly using command-line tools.


2. Viewing File Contents

Linux provides several commands to display file content.

The most commonly used commands are:

  • cat
  • less
  • more
  • head
  • tail

Each command has a different purpose.


3. The cat Command

The cat command stands for concatenate. It is commonly used to display the contents of a file.

Basic Syntax

cat filename

Example

cat /etc/hosts

This command prints the entire contents of the file to the terminal.


Display Multiple Files

cat can also display multiple files at once.

cat file1 file2

Number Lines with cat

cat -n filename

Example:

cat -n /etc/passwd

This displays the file with line numbers.


When cat is Used

System administrators often use cat to:

  • Quickly view small configuration files
  • Combine multiple text files
  • Display command output

However, it is not suitable for very large files because it prints everything at once.


4. The less Command

The less command is used to view large text files one screen at a time.

It is one of the most important commands for Linux administrators.

Syntax

less filename

Example

less /var/log/syslog

This opens the file in an interactive viewer.


Navigation in less

KeyAction
SpaceNext page
bPrevious page
Arrow keysMove up/down
/wordSearch for text
nNext search result
qQuit

Advantages of less

  • Efficient for large log files
  • Allows scrolling forward and backward
  • Allows searching inside files

5. The more Command

The more command is another tool used to display files page by page.

Syntax

more filename

Example:

more /etc/passwd

Differences Between more and less

Featuremoreless
Scroll forwardYesYes
Scroll backwardNoYes
Search textLimitedAdvanced
Recommended toolOlderPreferred

For modern Linux systems, less is usually preferred.


6. The head Command

The head command displays the first lines of a file.

Syntax

head filename

By default, it shows the first 10 lines.

Example

head /etc/passwd

Display Specific Number of Lines

head -n 5 filename

Example:

head -n 5 /var/log/syslog

This displays the first 5 lines of the file.


When head is Used

Administrators often use head to:

  • Quickly inspect the beginning of log files
  • Check file headers
  • Verify file format

7. The tail Command

The tail command displays the last lines of a file.

Syntax

tail filename

By default, it shows the last 10 lines.

Example:

tail /var/log/syslog

Show Specific Number of Lines

tail -n 20 filename

Example:

tail -n 20 /var/log/messages

Real-Time Log Monitoring

The -f option allows live monitoring of files.

tail -f filename

Example:

tail -f /var/log/syslog

This continuously displays new lines added to the file.

System administrators use this to monitor logs in real time.


8. Processing Text Files

Viewing files is only part of the task. Administrators also need to process text output.

Text processing means:

  • Filtering text
  • Formatting text
  • Extracting specific lines

Linux provides several commands to process text data.

Common tools include:

  • sort
  • uniq
  • wc
  • cut
  • tr

These commands are often used together with pipes (|).


9. The sort Command

The sort command sorts lines in a file.

Syntax

sort filename

Example:

sort users.txt

Reverse Sorting

sort -r filename

Example:

sort -r users.txt

Numeric Sorting

sort -n filename

Example:

sort -n numbers.txt

10. The uniq Command

The uniq command removes duplicate lines.

Syntax

uniq filename

However, uniq only removes adjacent duplicates, so it is usually used with sort.

Example:

sort users.txt | uniq

Count Duplicate Lines

uniq -c

Example:

sort users.txt | uniq -c

This shows how many times each line appears.


11. The wc Command

The wc command counts:

  • lines
  • words
  • characters

Syntax

wc filename

Example:

wc /etc/passwd

Output example:

45 120 3200 /etc/passwd

Meaning:

  • 45 lines
  • 120 words
  • 3200 characters

Useful Options

OptionDescription
wc -lCount lines
wc -wCount words
wc -cCount bytes

Example:

wc -l /etc/passwd

This counts how many user entries exist in the file.


12. The cut Command

The cut command extracts specific columns or characters from a file.

This is useful when processing structured text.

Example File Format

Many Linux files use colon-separated fields.

Example:

username:x:1000:1000:/home/user:/bin/bash

Extract a Field

cut -d: -f1 /etc/passwd

Explanation:

OptionMeaning
-d:delimiter is colon
-f1extract field 1

Output:

root
daemon
user

13. The tr Command

The tr command translates or replaces characters.

Convert Lowercase to Uppercase

tr a-z A-Z

Example:

cat file.txt | tr a-z A-Z

This converts text to uppercase.


Delete Characters

tr -d

Example:

tr -d '\r'

This removes carriage return characters.


14. Combining Commands with Pipes

Linux commands are often combined using pipes (|).

A pipe sends the output of one command to another command.

Example:

cat /etc/passwd | sort

Example:

cat /etc/passwd | wc -l

This counts how many lines exist in the file.


Example: Extract Usernames

cut -d: -f1 /etc/passwd

This extracts all usernames from the user database.


15. Viewing Compressed Logs

Sometimes text files are compressed.

Commands like zcat, zless, and zgrep allow viewing compressed files without extracting them.

Example:

zcat logfile.gz

Example:

zless logfile.gz

16. Best Practices for Viewing Large Files

When working with large files:

UseReason
lessEfficient browsing
headQuick preview
tailCheck recent entries
tail -fMonitor logs

Avoid using cat on extremely large files because it may flood the terminal.


17. Summary

For the Linux Essentials (LPI 010-160) exam, you should understand how to view and process text files using command-line tools.

Key commands include:

CommandPurpose
catDisplay file content
lessView large files interactively
morePage-by-page viewing
headShow beginning of file
tailShow end of file
sortSort text lines
uniqRemove duplicate lines
wcCount lines, words, characters
cutExtract fields from text
trTranslate or remove characters

These tools are essential for system administration tasks such as analyzing logs, inspecting configuration files, and extracting useful information from text data.

Buy Me a Coffee