When would you use `less` over `cat`?

QuestionsQuestions8 SkillsProDec, 20 2025
091

That is an excellent question! While both commands let you view file contents, they are used for very different situations.

The simplest way to remember it is: cat prints everything and finishes, while less lets you interact with the file.

Here are the specific reasons why you would choose less over cat:

1. File Size (The Biggest Reason)

  • cat is like dumping a bucket of water. If the file is 10,000 lines long, cat will fly through all of them until it reaches the end, leaving you at the bottom. You’ll have to scroll up manually with your mouse to find anything.
  • less is like a window. It only loads what fits on your screen. It handles massive files (even gigabytes) instantly without using up much memory.

2. Searching

In less, you can search for text inside the file.

  • While viewing a file in less, type / followed by a word (e.g., /Labby) and press Enter. It will highlight every time that word appears.
  • cat has no search feature; you would have to combine it with grep.

less allows you to move around using your keyboard:

  • Arrow Keys / Page Up / Page Down: Scroll through the file.
  • G: Jump to the very end.
  • g: Jump back to the very beginning.
  • q: Quit and go back to your prompt.

4. Screen Cleanliness

  • When you cat a file, the text stays on your terminal screen forever (until you type clear).
  • When you exit less (by pressing q), the file content usually disappears from your terminal, leaving your workspace clean.

Summary Table

Feature cat less
Best for... Small files, combining files Large files, reading logs
Speed Fast for small files Fast for all files
Search No Yes (using /)
Scroll Manual (mouse) Keyboard-controlled
Memory Loads whole file Loads one screen at a time

Try it out! In your terminal, try running less /etc/services. Use your arrow keys to move around, and press q when you're done. You'll immediately see why it's more powerful for reading!

0 Comments

no data
Be the first to share your comment!