less is a pager: it shows text one screen at a time and lets you move in both directions. It is the right tool for files too long for cat. It does not need to load the whole file before showing it, so it opens large files quickly.
Opening a file
$ less /etc/servicesThe screen fills with the start of the file. You are now inside less, so your keys control it and not the shell. Press q to quit.
Moving around
| Key | Action |
|-----|--------|
| Space or f | forward one screen |
| b | back one screen |
| Down arrow or j | forward one line |
| Up arrow or k | back one line |
| g | go to the first line |
| G | go to the last line |
Searching
Type / followed by a word and press Enter. less jumps to the next match and usually highlights it:
/sshPress n for the next match and N for the previous one in the opposite direction. Use ? instead of / to search backwards from where you are. Searches are case sensitive by default. Start with less -i to ignore case when your pattern is all lower case.
Reading command output
You can send the output of another command into less with a pipe, |:
$ ls -l /usr/bin | lessAll the same keys work.
Useful options
-Nshows line numbers.-Scuts long lines at the screen edge instead of wrapping them. Use the left and right arrows to see the rest.
For a log file that is still growing, press F inside less to follow new lines. Press Ctrl+C to stop following and return to normal browsing.
Press h at any time for the built-in help, and q to leave it. The man command also normally displays its pages with less, so these keys are worth learning.
Try these on real files in the streams and text lab.
Try it
- Open
/etc/serviceswithless. - Jump to the end with
Gand back withg. - Search for
http, then pressna few times. - Quit with
q, then runls -l /etc | less.
Check your understanding
less
All 5 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.