How To Use less Command To Read The Content Of A Text File In Linux, Unix or FreeBSD?


There are many ways to read a text file in Linux or Unix operating system. You can use a text editor or cat command to display the text in the terminal. The cat command is not a good option when you are using a text file that contains over thousand of lines. Using cat command will simply fill the terminal and you can not easily perform the operations like searching for a specific text.

less is a command-line utility that is also used to read a text file. It loads one screen at a time and allows both forward and backward navigation to the file which makes the task like reading, searching for a specific text easier We will see more about this in this article.

less Command

less is a program that is used to view the content of a  text file one screen at a time. It is available for different platforms including Linux, Unix, and Windows, etc. less command is very useful especially to read larger text files. It allows both forward and backward navigation through the file. less doesn’t have to read the entire file so it is faster than other text editors used in the terminal. Initially, It was written by Mark Nudelman during 1983-85.

Syntax of less command

less [options][filename]

Frequently used options

Options Description
-i Ignore case in searches
-f Forces non-regular file to open
-n Suppresses line number
-g Highlight only last match for searches
-m Shows more detailed prompt
-N To show line numbers
-X Leave file content on screen when less exits
-S Disable line wrap
-? Show help
-F Quit if the entire file fits on the first screen
-P It tells less to start at the first occurrence of pattern in the file

Frequently used commands

Commands Descriptions
space Next page
b Previous page
d Next half page
u Previous half page
v Edit content
j or enter Next line
k Previous line
Home Reach to the top of the file
End Reach to end of file
g or < First-line
G or > Last line
(n)G Move to linen
/pattern Forward search for pattern
?pattern Backward search for pattern
n Next search match
N Previous search match
ctrl+g or = For file information
:n Next file
:p Previous file
h help
q quit

To see the full list of options and commands that can be used with less command use the following command in your terminal –

man less

or

less --help

Usage with examples

To display the content of a text file, use the following command in your terminal-

less big.txt

Move to the next line or previous line by using up and down keys or use the commands as given in the above table.

To display the line numbers use -n option with less command use the following command-

less - n big.txt

Now if you want to search for a pattern use forward slash followed by text for example- /human happiness

Press the for the next pattern matched and use N to move to the previous pattern. Similarly for backward search use question mark followed by text for example- ?human happiness.

To access a new file in the terminal while reading, use the colon followed by n that is :n or use the colon followed by p that is :p to open the previous file. Press q if you want to close the file.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.