FactorPad
Faster Learning Tutorials

Linux less command summary with examples

less allows you to view and search text files.
  1. Purpose - Learn what less is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with less.
  4. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, October 4, 2016
Updated: July 23, 2018
In this tutorial, 5 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Next

~/ home  / tech  / linux essentials  / less command


The Linux less command

Beginner

Learn to view text files at the Linux command line.

Video Tutorial

Linux less command summary with examples (3:48)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our fifth word, or command to memorize is less from the category Text.

less allows you to view and search text files. In our last video we saw how the man command uses a text viewer, or pager, to view user manual files. Well, that text viewing program is less.

If it helps, think of commands as mini programs and most have this structure. First, type the command, second the -option(s) and third the argument(s).

The less command has over 30 options and its argument is typically the file you intend to read. Many of the options refer to scrolling and searching for patterns in text.

For now, we only have time to focus on two simple options. First is --help, which provides a tidy screen showing scrolling options. Since there are many text viewers and editors in Linux, I suggest sticking to, and learning, only one or two.

Second is -N, for viewing line numbers.

So why is less an important command? Well, in many flavors of Linux, less is the default viewer for man pages, and viewing text is an essential task in Linux. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows 10.

Here we go, let's start with less --help and then review this later.

$ less --help SUMMARY Commands marked with * may be preceded by a number, _N. Notes in parentheses indicate the behavior if _N is given. A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K. h H Display this help. q :q Q :Q ZZ Exit. --------------------------------------------------------------------------- MOVING e ^E j ^N CR * Forward one line (or _N lines). y ^Y k ^K ^P * Backward one line (or _N lines). f ^F ^V SPACE * Forward one window (or _N lines). b ^B ESC-v * Backward one window (or _N lines). z * Forward one window (and set window to _N). w * Backward one window (and set window to _N). ESC-SPACE * Forward one window, but don't stop at end-of-file. d ^D * Forward one half-window (and set half-window to _N). u ^U * Backward one half-window (and set half-window to _N). ESC-) RightArrow * Left one half screen width (or _N positions). ESC-( LeftArrow * Right one half screen width (or _N positions). F Forward forever; like "tail -f". r ^R ^L Repaint screen. R Repaint screen, discarding buffered input. --------------------------------------------------- Default "window" is the screen height. Default "half-window" is half of the screen height. --------------------------------------------------------------------------- ...190+ lines trimmed

Next, let's use less to view a file I created for this exercise, by typing less and the filename video5.txt. There it is.

This is the first line This is the second line This is the thrid line This is the fourth line This is the fifth line # A file for video 5 on less to demonstrate the -N option video5.txt (END)

Hit q. Let's do something different here. Type less -N and the same file name video5.txt.

1 This is the first line 2 This is the second line 3 This is the thrid line 4 This is the fourth line 5 This is the fifth line 6 7 # A file for video 5 on less to demonstrate the -N option video5.txt (END)

See how it numbered the lines? This can be helpful, especially when reviewing code. Hit q again.

A Final Tip

Okay now you know how to use less. And you know the syntax for commands, options and arguments.

One last tip about the less command. The footer of less -h summary reminds you of the two most important keys, h for help and q for quit.

Okay, thanks for visiting today. I hope this was a fun introduction to the less command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

If you learned something please consider subscribing.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the man command, hit Back.
  • To move on to command #6 pwd, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / less command



 
 
Keywords:
linux less
less pager
linux pager
linux page viewer
view text
command line pager
unix less
linux less search
linux less line number
linux less viewer