How to Use the Less Command in Linux

From viewing large files to highlighting text, the Linux 'less' command offers an interactive and efficient way to navigate and search file contents.

At its core, less command provides an interactive environment to view the contents of a file, enabling users to scroll line-by-line or page-by-page. Unlike other tools such as cat, which simply dumps the entire file content to the console, less ensures easy navigation, search functionalities, and even offers features like text highlighting.

Especially useful for peering into large files or logs, its efficiency and straightforwardness have rendered less an indispensable command for both newcomers and seasoned Linux professionals.

General syntax for less command:

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

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

Example:

When you execute less -N example.txt, the command less -N will open the file example.txt in the less pager with line numbers displayed at the beginning of each line. Here is a sample output for what you might see:

     1 This is line one of the file.
     2 Line two comes after line one.
     3 The third line is this one.
     4 And here's line four.
     5 This example has five lines in total.
2. Search for text in a file
less -p [word] [filename]

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

Example:

The command less -p apple fruit.txt would open the file fruit.txt and display the contents starting from the first occurrence of the word “apple.”

Let’s say the content of fruit.txt is:

orange
banana
apple
grape
apple
cherry

Executing less -p apple fruit.txt would open the file in a pager starting from the line with the first occurrence of “apple,” displaying something like:

apple
grape
apple
cherry
3. Display multiple files
less [file1] [file2] [file3]

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

4. Reduce blank lines
less -s [filename]

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

Example:

The output of the less -s file1.txt command will simply be the contents of file1.txt, but displayed one screen at a time in your terminal, and with multiple consecutive blank lines condensed into single blank lines.

Suppose file1.txt contains the following:

This is line 1

This is line 3

This is line 5

This is line 7

If you run less -s file1.txt, you would see:

This is line 1

This is line 3

This is line 5

This is line 7
5. 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.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail