The less command
The less command opens a long file one screen at a time…
The less command opens a long file one screen at a time, so you can scroll and search it; press q to quit.
Taught in Intermediate, lesson 17
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
less [-N] [FILE]...
View a file one screen at a time. Opens a long file so you can read it a page at a time: Space or f goes down a page, b goes back, the arrow keys move a line, /word searches forward, n finds the next match, g and G jump to the start and the end, and q quits. In this practice shell less prints the whole file instead and says so once; when its output goes into a pipe or a file, the real less does the same and just passes the text on.
How to use less
Read a long file
Space goes down a page, b goes back, and the arrow keys move a line. This practice terminal shows the whole file instead, so scroll up to read it.
less notes.txtNote 1: call Sam Note 2: renew the library books Note 3: back up the laptop Note 4: book the dentist Note 5: pay the electricity bill Note 6: buy coffee Note 7: water the plants Note 8: call Sam Note 9: renew the library books Note 10: back up the laptop Note 11: book the dentist Note 12: pay the electricity bill Note 13: buy coffee Note 14: water the plants
Search inside less
Type / and a word, then Enter, to jump to the next match; n finds the next one and N the one before. g goes to the start and G to the end.
less app.log08:00 INFO started 08:05 ERROR disk not found 08:06 INFO retrying 08:07 error: timeout 08:10 INFO ready 08:15 WARN slow request
Page through a command’s output
Pipe any long output into less.
ls -lR /var | less/var: total 8 drwxr-xr-x 3 root root 4096 Jan 12 09:00 log drwxrwxrwx 2 root root 4096 Jan 12 09:00 tmp /var/log: total 12 drwxr-xr-x 2 root root 4096 Jan 12 09:00 apt -rw-r--r-- 1 root root 49 Jan 12 09:00 auth.log -rw-r--r-- 1 root root 81 Jan 12 09:00 syslog /var/log/apt: … and 5 more lines
Line numbers: less -N
less -N data.csv 1 name,score
2 Ava,82
3 Leo,91
4 Mia,77
Options
| -N | number the lines |
|---|---|
| -S | cut long lines at the edge of the screen instead of wrapping them |
More examples
less -N app.log 1 08:00 INFO started
2 08:05 ERROR disk not found
3 08:06 INFO retrying
4 08:07 error: timeout
5 08:10 INFO ready
6 08:15 WARN slow request
grep -i error app.log | less08:05 ERROR disk not found 08:07 error: timeout
Questions about less
How do I exit less?
Press q. If you started typing a search, press Esc first, then q.
What is the difference between less and more?
Both show a file a page at a time. more mostly goes forward only; less can go back, search both ways and open large files quickly, which is why it is the usual choice.
What is the difference between less and cat?
cat prints the whole file at once, which scrolls past for a long file. less shows one screen and waits for you.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.