π₯οΈ 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 :
