Skip to content

Linux Foundations / Find your way around Linux ยท Step 19 of 63

Lesson: lessSwitch
โ† Linux Foundations

Shell and files

less

Read long files comfortably with less: scrolling, searching and quitting.

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/services

The 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:

/ssh

Press 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 | less

All the same keys work.

Useful options

  • -N shows line numbers.
  • -S cuts 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

  1. Open /etc/services with less.
  2. Jump to the end with G and back with g.
  3. Search for http, then press n a few times.
  4. Quit with q, then run ls -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.