πŸ“‘Linux/Unix: File Viewing, Redirection & Filters
Estimated reading: 3 minutes 115 views

πŸ–₯️ 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:

KeyAction
↑/↓Scroll line up/down
SpacePage down
bPage up
/patternSearch forward
qQuit 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 cat for quick views, less for scrollable output.
  • head and tail are great for peeking at the top or bottom of files.
  • tac shows content in reverse, useful for logs and debugging.
  • less is 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 :
Share

πŸ”΅ Linux/Unix: Viewing Tools (cat, less, more, head, tail, tac)

Or Copy Link

CONTENTS
Scroll to Top