Linux/Unix: Viewing Tools β cat, less, more, head, tail, tac Explained
Introduction β Why Learn Linux File Viewing Tools?
When working on Linux/Unix systems, it’s essential to view, inspect, and analyze text files directly from the terminal. Tools like cat, less, more, head, tail, and tac allow you to do this efficiently without needing a GUI-based editor.
In this guide, youβll learn:
- The role of each command in viewing file content
- Syntax, usage, and practical examples
- How to scroll, filter, or reverse-read logs and large files
cat β Concatenate and Display File Content
Syntax:
cat [options] filename
Description:
- Displays the full contents of a file on standard output.
- Good for small files or quick previews.
Examples:
cat file.txt # View entire file
cat file1 file2 > merged # Combine files into one
Use cat -n to show line numbers:
cat -n file.txt
less β Scrollable File Viewer (Recommended)
Syntax:
less filename
Description:
- Opens file in a scrollable viewer.
- Supports forward and backward navigation.
- Does not load entire file into memory, making it ideal for large files.
Navigation Tips:
| Key | Action |
|---|---|
β/β | Scroll line up/down |
Space | Page down |
b | Page up |
/pattern | Search forward |
q | Quit viewer |
more β Basic Pager
Syntax:
more filename
Description:
- Displays file page-by-page from top to bottom.
- Less powerful than
less, but widely available.
Example:
more largefile.txt
Press q to exit, Space for next page.
π§’ head β View the Top of a File
Syntax:
head [options] filename
Description:
- Shows the first 10 lines by default.
Examples:
head file.txt # First 10 lines
head -n 20 file.txt # First 20 lines
Useful for config or log previews.
π£ tail β View the Bottom of a File
Syntax:
tail [options] filename
Description:
- Displays the last 10 lines by default.
- Can follow file updates in real-time (like log monitoring).
Examples:
tail file.txt # Last 10 lines
tail -n 25 file.txt # Last 25 lines
tail -f /var/log/syslog # Real-time log updates
Combine with grep for filtered monitoring:
tail -f logfile.log | grep "ERROR"
tac β Display File in Reverse
Syntax:
tac filename
Description:
- Displays the file line-by-line in reverse order (bottom to top).
- Great for reading logs or reversed sequences.
Example:
tac error.log
Summary β Recap & Next Steps
Each of these tools provides a fast, memory-efficient way to explore file content from the terminal. From quick views (cat) to advanced paging (less) and log monitoring (tail -f), these commands form the backbone of Linux file inspection.
Key Takeaways:
- Use
catfor quick views,lessfor scrollable output. headandtailare great for peeking at the top or bottom of files.tacshows content in reverse, useful for logs and debugging.lessis preferred for large files and supports searching.
FAQs
When should I use less instead of cat?
Use less for large filesβit allows paging and doesnβt flood your terminal.
How can I monitor real-time logs in Linux?
Use:
tail -f /path/to/logfile
Whatβs the difference between more and less?
less allows both forward/backward scrolling and searching; more only pages forward.
Can tac be used on binary files?
No. It’s designed for reversing line-based text files, not binaries.
How do I view only the first 5 lines of a file?
Use:
head -n 5 filename
Share Now :
