📖 Command: less (View File Content)
While cat dumps the entire content of a file into your terminal at once, less is a pager. It allows you to open a file and navigate through it line by line or page by page without loading the entire file into memory.
1. The "Why"
If you try to use cat on a file with 10,000 lines (like a system log or a large file), your terminal will be flooded. You'll lose your place and have to scroll manually for ages.
less is smarter. It is fast, efficient, and allows you to search for keywords while you are reading. In the Linux world, there is a famous saying: "Less is more", referring to the fact that less replaced an older, more limited command called more.
2. Basic Syntax
less [OPTIONS] FILENAME
Once inside less, the command doesn't end immediately. You enter an "interactive mode" where you can move around using your keyboard.
3. Navigation Controls (Inside less)
Since less is interactive, you need to know the keyboard shortcuts to move:
| Key | Action |
|---|---|
| Space / Page Down | Move down one full screen. |
| b / Page Up | Move up one full screen. |
| Enter / Down Arrow | Move down one line. |
| y / Up Arrow | Move up one line. |
| G | Go to the very end of the file. |
| g | Go to the very beginning of the file. |
/ followed by word |
Search for "word" in the document. |
| n | Find the next occurrence of your search. |
| q | Quit and return to the terminal. |
4. Practical Examples
A. Reading a Large Code File
If you are reviewing your source code :
less main.java
B. Viewing Command Output
You can "pipe" the output of another command into less if that output is too long. For example, to see all available commands on your system:
compgen -c | less
C. Opening Multiple Files
You can open several files at once and switch between them:
less chapter1.tex chapter2.tex
- Type
:nto go to the next file. - Type
:pto go to the previous file.
D. Keeping the File Open (Follow Mode)
This is a "pro" move for system admins. If you use +F, less will stay open and automatically show new lines as they are added to the file (like a live log):
less +F /var/log/syslog
5. Pro-Tips
- Search and Highlight: When you search using
/,lesshighlights all matches. To clear the highlight, you can usually just type a search for something that doesn't exist. - Line Numbers: If you want to see exactly which line you are on, start it with the
-Nflag:less -N config.conf - Arch Linux Context: You will use
lessconstantly to read documentation or view the output ofjournalctl(the system log viewer in Arch), which actually useslessas its default display tool. - Don't Panic: If you get stuck and can't get back to your terminal prompt, just hit q.
6. Summary Reference
| Goal | Action/Command |
|---|---|
| Open a file | less filename |
| Search forward | /text_to_find |
| Go to end of file | G |
| Toggle line numbers | less -N filename |
Exit less |
q |