Post

How to Display File Contents in Terminal: 5 Essential Commands

Master terminal file viewing with cat, less, head, tail, and more commands for efficient text file inspection.

How to Display File Contents in Terminal: 5 Essential Commands

Five commands cover every file viewing scenario in the terminal.

Quick Display: cat

Display entire file content instantly:

1
cat filename.txt

For large files, pipe to more for page-by-page viewing:

1
cat filename.txt | more

Or use less for better navigation (scroll with arrow keys, quit with q):

1
cat filename.txt | less

First Lines: head

View the beginning of a file. Default shows 10 lines:

1
head filename.txt

Specify exact line count:

1
2
head -20 filename.txt
head -n 50 filename.txt

Useful for checking log file headers or configuration file documentation.

Last Lines: tail

Display file end. Default shows 10 lines:

1
tail filename.txt

Specify line count:

1
2
tail -30 filename.txt
tail -n 100 filename.txt

Monitor live updates with -f (follow mode):

1
tail -f /var/log/system.log

Essential for watching log files in real-time. Press Ctrl+C to stop.

Interactive Viewing: less

Best for large files or when you need search capability:

1
less filename.txt

Navigation shortcuts:

  • Space — next page
  • b — previous page
  • /pattern — search forward
  • ?pattern — search backward
  • q — quit

Less loads files efficiently without reading entire content into memory.

Multiple Files

Display multiple files with separator lines:

1
cat file1.txt file2.txt file3.txt

Compare first lines across files:

1
head -5 *.log

Monitor multiple log files simultaneously:

1
tail -f app.log error.log

When to Use Each

  • cat — Small files, quick checks, piping to other commands
  • less — Large files, searching, backward navigation
  • head — File structure validation, sample data inspection
  • tail — Log monitoring, recent entries, debugging
  • more — Legacy systems where less unavailable

Combined with grep

Search while viewing:

1
2
3
cat large-file.txt | grep "error"
tail -100 app.log | grep "WARNING"
head -50 config.yaml | grep "port"

All commands work with any text file regardless of extension (.txt, .log, .json, .xsh, .conf).

☕ Support My Work

If you found this post helpful and want to support more content like this, you can buy me a coffee!

Your support helps me continue creating useful articles and tips for fellow developers. Thank you! 🙏

This post is licensed under CC BY 4.0 by the author.