Viewing File Contents¶
Linux provides powerful commands to view, inspect, and monitor file contents. Whether you're reading configuration files, analyzing application logs, or debugging production issues, mastering these commands is essential for every Linux administrator, DevOps Engineer, and Cloud Engineer.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- View file contents efficiently
- Read large log files
- Display specific lines from files
- Monitor files in real time
- Compare different file viewing commands
- Choose the right command for different scenarios
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 Lessons 1–4
Why Learn File Viewing Commands?¶
Linux systems store everything in files.
Examples include:
- Configuration files
- Application logs
- Shell scripts
- Kubernetes YAML files
- Docker Compose files
- Service logs
As a Linux administrator, reading files is one of the most common daily tasks.
Sample File¶
Throughout this lesson we'll use the following file.
Line 1: Linux
Line 2: Docker
Line 3: Kubernetes
Line 4: Terraform
Line 5: Ansible
Line 6: GitLab
Line 7: Jenkins
Line 8: Prometheus
Line 9: Grafana
Line 10: REBASH Academy
cat¶
The cat command displays the complete contents of a file.
Syntax
Example
Display multiple files
Display line numbers
Useful when viewing:
- Configuration files
- Small scripts
- README files
less¶
less is one of the most useful Linux commands.
Unlike cat, it lets you scroll through large files.
Navigation
| Key | Action |
|---|---|
| Space | Next Page |
| b | Previous Page |
| ↑ ↓ | Scroll |
| / | Search |
| n | Next Match |
| q | Quit |
Perfect for:
- System logs
- Large configuration files
- Long text files
more¶
Older pager command.
Displays one screen at a time.
Although still available, most Linux administrators prefer less.
head¶
Display the beginning of a file.
Default:
Display first five lines
Production example
tail¶
Display the end of a file.
Display last five lines
Production example
Real-Time Monitoring¶
One of the most useful Linux commands.
Linux continuously displays new lines as they are written.
Widely used for:
- Application logs
- Kubernetes logs
- Web server logs
- Docker logs
Stop monitoring:
tac¶
Reverse of cat.
Displays the file from bottom to top.
Useful when newest information is at the end of the file.
nl¶
Display line numbers.
Output
Useful when discussing specific lines in configuration files.
file¶
Determine the file type.
Example output
Check executable
wc¶
Count lines, words and characters.
Example
Only count lines
Count words
strings¶
Display printable text from binary files.
Useful when investigating executables.
Command Comparison¶
| Command | Best Used For |
|---|---|
| cat | Small files |
| less | Large files |
| more | Basic paging |
| head | Beginning of file |
| tail | End of file |
| tail -f | Live log monitoring |
| tac | Reverse output |
| nl | Line numbers |
| file | Identify file type |
| wc | Count lines and words |
Production Example¶
A web application is failing.
Check logs:
Monitor new errors:
View configuration:
Verify line numbers:
This workflow is common during production troubleshooting.
Production Perspective¶
These commands are used daily by engineers.
Examples
View Kubernetes YAML
Read Docker logs
Monitor application
Read configuration
Count log entries
Hands-on Lab¶
Task 1¶
Create a file.
Add:
Press:
Task 2¶
Display file.
Task 3¶
Display first three lines.
Task 4¶
Display last two lines.
Task 5¶
Display line numbers.
Task 6¶
Count lines.
Task 7¶
Open using less.
Quit:
Task 8¶
Display reverse order.
Command Deep Dive¶
| Command | Purpose | Common Options | Production Example |
|---|---|---|---|
| cat | Display files | -n | View configuration |
| less | Scroll files | Search / | Read logs |
| head | Beginning | -5 | Verify headers |
| tail | End | -10 | View recent logs |
| tail | Live logs | -f | Monitor applications |
| nl | Line numbers | Default | Debug configs |
| wc | Count | -l -w | Count log entries |
| file | File type | Default | Verify downloads |
Mini Challenge¶
Create a file named:
Add at least 10 Linux commands.
Now:
- Display the first 5 lines.
- Display the last 3 lines.
- Count the number of lines.
- Display line numbers.
- View the file using
less. - Display the file in reverse order.
Best Practices¶
- Use
lessinstead ofcatfor large files. - Use
tail -ffor monitoring logs. - Use
headto inspect file headers. - Use
wcto quickly summarize file contents. - Learn keyboard shortcuts in
less.
Common Mistakes¶
❌ Using cat on huge log files.
✅ Use:
or
❌ Opening multi-GB log files in editors.
✅ Use:
❌ Forgetting to quit less.
✅ Use:
Interview Questions¶
Beginner¶
- What does
catdo? - Difference between
headandtail? - Which command monitors logs in real time?
- What is
lessused for?
Intermediate¶
- Why is
lesspreferred overcatfor large files? - Explain
tail -f. - How do you count the number of lines in a file?
- What does the
filecommand do?
Architect Level¶
- How would you investigate a production application that is generating errors?
- Which commands would you use to analyze a 5 GB log file?
- Why is
tail -fessential for production monitoring?
Summary¶
In this lesson, you learned:
- Displaying file contents
- Reading large files
- Viewing file headers and footers
- Monitoring logs in real time
- Counting lines
- Identifying file types
- Choosing the right command for different situations
These commands form the foundation of Linux troubleshooting and system administration.
Key Takeaways¶
catis ideal for small files.lessis the preferred tool for large files.headdisplays the beginning of a file.taildisplays the end of a file.tail -fmonitors log files in real time.wccounts lines, words, and characters.- Choosing the right command improves productivity and troubleshooting efficiency.
What's Next?¶
Searching Files and Directories
In the next lesson, you'll learn:
findlocatewhichwhereistype- Searching by name, size, permissions, owner, and modification time
- Real-world search techniques used by Linux administrators