DEV Community

Sreekanth Kuruba
Sreekanth Kuruba

Posted on

Viewing Files in Linux Explained Simply (cat, less, head, tail & wc)

Introduction

Finding a file is only half the job.

The next step is reading it.

Whether it's a configuration file, a log, or a text document, Linux provides several commands to view files efficiently.

Each command has a different purpose.

Knowing which one to use can save time and make troubleshooting much easier.

Let's simplify them.


Why Not Just Use cat for Everything?

Many beginners learn:

cat filename
Enter fullscreen mode Exit fullscreen mode

Then use it for every file.

It works...

Until the file has 50,000 lines.

Linux provides better tools depending on what you're trying to do.


1. View the Entire File with cat

The cat command displays the complete contents of a file.

Example:

cat config.txt
cat file1.txt file2.txt     # Combine files
cat > notes.txt             # Create new file
Enter fullscreen mode Exit fullscreen mode

Best for:

  • Small files
  • Quick checks
  • Combining files

2. Read Large Files with less

Large files are difficult to read with cat.

Use:

less /var/log/syslog
less /etc/nginx/nginx.conf
Enter fullscreen mode Exit fullscreen mode

Useful shortcuts:

  • Space → Next page
  • b → Previous page
  • /text → Search
  • q → Quit

Best for:

  • Configuration files
  • Large logs
  • Documentation

3. View the Beginning with head

Sometimes you only need the first few lines.

head file.txt
head -20 file.txt          # First 20 lines
Enter fullscreen mode Exit fullscreen mode

Useful for:

  • Checking file headers
  • Previewing files

4. View the End with tail

Need the latest log entries?

tail file.txt
tail -20 file.txt          # Last 20 lines
Enter fullscreen mode Exit fullscreen mode

Perfect for log files.
Checking recent changes.


5. Monitor Files Live with tail -f

One of the most-used Linux troubleshooting commands.

tail -f /var/log/nginx/error.log
tail -f app.log
Enter fullscreen mode Exit fullscreen mode

As new lines are written, they appear instantly.

Stop with:

Ctrl + C
Enter fullscreen mode Exit fullscreen mode

Useful for:

  • Monitoring logs
  • Watching application output
  • Debugging services

6. Count with wc

Want to know how big a file is?

wc file.txt
wc -l file.txt             # Count only lines
wc -w file.txt             # Count words
wc -m file.txt             # Count characters
Enter fullscreen mode Exit fullscreen mode

Useful for:

  • Scripts
  • Reports
  • Log analysis

Real-World Example

An application isn't starting.

First, check the configuration file:

# First, check the configuration file:
less /etc/nginx/nginx.conf

# Monitor the error log:
tail -f /var/log/nginx/error.log

# Need to know how many errors occurred?
grep -i error app.log | wc -l

Enter fullscreen mode Exit fullscreen mode

This is a common workflow when troubleshooting Linux systems.


Common Beginner Mistakes

  • Using cat for huge files
  • Forgetting less supports searching
  • Not using tail -f for live logs
  • Thinking head and tail only work with 10 lines

Simple Mental Model

Think of these commands like reading a book.

Command Think of it as...
cat Read the whole book
less Read page by page
head Read the first page
tail Read the last page
tail -f Watch new pages being written
wc Count pages, words, or characters

Summary

In this guide you learned:

  • cat → View complete files
  • less → Read large files
  • head → View the beginning
  • tail → View the end
  • tail -f → Monitor files live
  • wc → Count lines, words, and characters

Why This Matters

Configuration files and logs are everywhere in Linux.

Knowing how to read them efficiently is an essential skill for developers, system administrators, and DevOps engineers.

The right command can save time and make troubleshooting much easier.


Next Post

File Compression in Linux Explained Simply (tar, gzip, zip & unzip)


Question for You

Which command do you use the most when reading files in Linux?

  • cat
  • less
  • tail
  • Or something else?

Top comments (0)