How to Use the Less Command in Linux

This command works similarly tail in which it can be used to view content of files, except you can also scroll up or down of the file, or jump to specific line with some shortcut keys.

General syntax for less command:

$ less [FILE]
Display line numbers
less -N [file]

The -N option allows you to see line numbers in the output of the less command.

Example: Using this option with the ExampleFile.txt.

display line numbers
Search for text in a file
less -p [word] [filename]

This command helps you find a word or phrase in a file.

Example: Searching for the word “example” in the Example.txt

search for text in file
Display multiple files
less [file1] [file2] [file3]

Use this command to open multiple files by typing the input file names one by one.

Example: Let’s display several files at once named name1.txt, name2.txt, name3.txt.

display multiple files
Reduce blank lines
less -s [filename]

This option reduces a series of blanks lines and replaces them with a single blank line.

Example: There are a couple of consecutive blank lines in the randomtext.txt file, let’s see how less handles them with the -s option.

reduce blank lines
Keep content in terminal after exit
less -X [file]

By default, when you exit less, the contents of the file will be removed from the screen. To keep them on the screen, use the -X option.

Example: Leaving the contents of the sometext.txt file in the terminal.

keep content in terminal after exit