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

OptionsDescription
-iIgnore case in searches
-fForces non-regular file to open
-nSuppresses line number
-gHighlight only last match for searches
-mShows more detailed prompt
-NTo show line numbers
-XLeave file content on screen when less exits
-SDisable line wrap
-?Show help
-FQuit if the entire file fits on the first screen
-PIt tells less to start at the first occurrence of pattern in the file

Frequently used commands

CommandsDescriptions
spaceNext page
bPrevious page
dNext half page
uPrevious half page
vEdit content
j or enterNext line
kPrevious line
HomeReach to the top of the file
EndReach to end of file
g or <First-line
G or >Last line
(n)GMove to linen
/patternForward search for pattern
?patternBackward search for pattern
nNext search match
NPrevious search match
ctrl+g or =For file information
:nNext file
:pPrevious file
hhelp
qquit

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.