Module 01 - Command Line Fundamentals
Lesson 03 - Viewing Files and Getting Help
Estimated time: 45–60 minutes
Difficulty: Beginner
Prerequisites: - Module 00 - Introduction to Linux - Lesson 01 - The Linux Shell and Navigation - Lesson 02 - Working with Files and Directories
Overview
Linux administrators spend a large amount of time reading configuration files, reviewing logs, and finding information about commands.
Unlike graphical operating systems, Linux provides powerful command-line tools for viewing and searching information quickly.
This lesson introduces the tools required to:
- View file contents
- Read large files
- Monitor changing files
- Find command documentation
- Understand command options
- Use command history
Learning Objectives
By the end of this lesson you will be able to:
- Display the contents of files
- Read large text files safely
- View the beginning and end of files
- Monitor log files
- Use Linux manual pages
- Find help for commands
- Review previously executed commands
Creating Test Files
Before learning how to view files, create some example files.
Create a directory:
mkdir file-demo
Move into it:
cd file-demo
Create a file:
touch example.txt
Add some content:
echo "Linux command line training" > example.txt
Check the file exists:
ls
Viewing File Contents
The cat Command
The simplest way to display a file is using:
cat filename
Example:
cat example.txt
Output:
Linux command line training
cat is useful for small files.
Viewing Line Numbers
Use:
cat -n filename
Example:
cat -n example.txt
Output:
1 Linux command line training
Line numbers are useful when troubleshooting configuration files.
Reading Large Files
Large files can contain thousands of lines.
Examples:
- Log files
- Application configuration files
- System reports
Using cat on very large files can make the terminal difficult to use.
For large files, use:
less
The less Command
Example:
less /var/log/syslog
less allows you to scroll through files.
Useful keys:
| Key | Action |
|---|---|
| Space | Next page |
| b | Previous page |
| ↑ | Move up |
| ↓ | Move down |
| / | Search |
| q | Quit |
Example search:
/error
finds occurrences of the word:
error
Viewing the Beginning of Files
The head command displays the first lines of a file.
Example:
head example.txt
By default it shows the first 10 lines.
To show a specific number:
head -20 example.txt
Displays the first 20 lines.
Viewing the End of Files
The tail command displays the last lines of a file.
Example:
tail example.txt
Useful for checking recent log entries.
Example:
tail /var/log/syslog
Monitoring Log Files
Linux systems generate many log files.
Examples:
/var/log/syslog
/var/log/auth.log
To watch a log file as it changes:
tail -f /var/log/syslog
The -f option means:
follow
The terminal will continue displaying new entries.
Exit with:
Ctrl + C
Getting Help in Linux
Linux has several built-in ways to find help.
The man Command
man displays the manual page for a command.
Example:
man ls
Output includes:
- Command description
- Available options
- Examples
- Related commands
Searching Manual Pages
Example:
man -k password
Searches available manual pages containing:
password
Command Help Options
Many commands provide their own help.
Example:
ls --help
Output:
Usage:
ls [OPTION]... [FILE]...
Common help options:
--help
-h
-help
Finding Command Locations
The which command shows where a command is installed.
Example:
which ls
Output:
/usr/bin/ls
Understanding Commands
The whatis command provides a short description.
Example:
whatis ls
Output:
ls (1) - list directory contents
Command History
Linux stores previously executed commands.
View your history:
history
Example:
101 ls
102 cd /etc
103 systemctl status ssh
Re-running Previous Commands
Use the up arrow:
↑
to cycle through previous commands.
Searching History
Press:
Ctrl + R
Then type part of a previous command.
Example:
ssh
Linux will search your command history.
Combining Commands
Linux commands can be combined using pipes.
A pipe:
|
passes the output of one command into another.
Example:
ls /etc | less
This sends the output of ls into less.
Useful when commands produce a lot of output.
Practical Exercise
Task 1 - Create a Log File
Create:
touch application.log
Add some content:
echo "Application started" >> application.log
echo "Connection successful" >> application.log
echo "Error detected" >> application.log
Task 2 - View the File
Display the contents:
cat application.log
Task 3 - Use Different Viewing Commands
Try:
head application.log
tail application.log
less application.log
Task 4 - Find Help
Find information about:
grep
using:
man grep
and:
grep --help
Task 5 - Review History
Display your command history:
history
Search for a previous command:
Ctrl + R
Knowledge Check
-
Which command displays the contents of a small file?
-
Which command is better for reading large files?
-
What command displays the last lines of a file?
-
How do you monitor a log file as it changes?
-
What command displays documentation for Linux commands?
-
What does the pipe character (
|) do?
Summary
In this lesson you learned:
✓ How to display file contents using cat
✓ How to read large files using less
✓ How to view file beginnings and endings using head and tail
✓ How to monitor logs using tail -f
✓ How to use Linux manual pages
✓ How to find command help
✓ How to use command history
✓ How to combine commands using pipes
Next lesson:
Lesson 04 - Practical Command Line Lab