less command in Linux with examples.

Let’s learn about the less command in Linux with examples, mainly used to view the content, and it divide the content into pages.

We can search the pattern we want, we can switch between the files, we can check the big files like logs files.

How to view the content by using less command.

To see the content of the file using less command we will use the following command.

Command:
# less system_logs

So, this will divide the content into pages and suppose we have to quit the file then will use “ESC :q”.

How to display the number of lines using less command in Linux.

So to display the Number of line use -N option with the less command

Command:
# less -N SYSTEM_LOGS
How to move around the pages using less command.

So, guys let’s play with the files via less command, many options we can use just have a look for a few of them.

So, first we will look at how we can move between the pages.

If we have to move by one page then we will “press the SPACE” key, and the same we have to come back forward then we will “press b” key.

TO MOVE DOWNWARD PRESS SPACE KEY TO MOVE UPWARD PRESS b KEY

So, what if we have to move one line up and down then use ARROW keys the same we can use j and k.

USE j to move forward by one line and k to move backward by one line.

Now, what if we have to move 5 lines upward and downward let’s use the same j and k.

So, to move forward by 5 lines then use :5 and press j and same we have to move backward press :5 k

How to go at the end of the file by using less command.

So, now we will use the key SHIFT+g or G, this option will come at the end of the file.

How to go at the start of the file by using less command.

For this we will press g key, this will come up at the first line of the file.

How to go at the particular line number using less command.

So, now we have to go at 43 line then will just use :43 and then press g.

How to Search word/content/pattern using less command in Linux.

Now we will look into the search pattern using “/”, “?”.

Also, search two words have space in that, search path on the content.

So, now from the logs we have to search the “word DHCPREQUEST”.

We will use “/pattern” means it will be like “/DHCPREQUEST”.

Now suppose we have to search more words with that same name we will use n to move forward and shift+n to move backward.

To search the content use /content_that_we_have_search, press n to move downward and N to move upward.

So, what if we are at the END of the file and we have to search the same word?

So, now we have to search the word “bound” and I am at the END of the file.

I will use ?pattern means now it will look like ?bound, and if I have to search the next word with the name bound.

So will press n to search in an upward direction and N to search in a downward direction.

To search the content use ?content_that_we_have_search, press n to move upward and N to move downward.

Now, suppose we have to search two words like “Session Started” into the log files.

So, we will use /Session\ Started to search the above word.

So, what if we have to search the timestamp in the logs “May 18 10:10:01” for that we will /May\ 18\ 10:01:01

What if there will be a path into the log file like /hom/tla? Let’s see how we can search that kind of pattern.

So, now our pattern will be like “/\/home/tla” and then press enter key.

How to Examine File using less command in Linux.

So, now we will look multiple log file with using less command.

Also, we will look how we can add a file inside the less command.

We have the file named SYSTEM_LOGS and we have to add the first_file, without quit the less command.

So, here just press E(shift+e) this will prompt to the screen for “:Examine:” and then enter the file name first_file and press enter key.

you will find that you are into the first_file, let’s have a look at how we can switch between the files.

press E(shift+e) this will prompt to the screen for “:Examine:” and then enter the file name first_file and press enter key.

Command:
# less SYSTEM_LOGS first_file

So, now we have 2 files first is SYSTEM_LOGS, and the first_file just press ESC :n for the next file and :p for the previous file.

How to edit the file using less command.

So finally, we will edit the file when we are using less command just press v you will find that you are in the nano editor.

Any issue you can ask me in the comment section. Thanks to read the post keep supporting us!

To learn more basic command refer, also find the practice link.

Cheatsheet of less command in Linux
How to view the content by using less command

Syntax:
# less [file]

Example:
#less system_logs
How to view the content of a multiple file.
Syntax:
# less [file1 file2 ... fileN]

Example:
#less system_logs first_file second_file
Display the number of lines using less command in Linux.

Syntax:
# less -N [file]

Example:
#less -N system_logs
How to go one page up and one page down.

press SPACE key to scroll forwards.
press b key to scroll backwards
How to go one line forward and one line backward.

press  j to move forward by one line
press  k to move backward by one line
How to go particular line forward and  backward.

press  j to move forward and press  k to move backward
:5j to move 5 lines forward
:8k to move 8 line backward
How to go at the end of the file by using less command.

press shift+g or G
How to go at the start of the file by using less command.

press g
How to go at the particular line number using less command.

press :number line and then press g

Example:
:43g **This will go at the line number 43**
How to search the content using less command.

with / option

Example:
Suppose you have to search a word tastethelinux then
/tastethelinux

with ? option

Example:
Suppose you have to search a word tastethelinux then
?tastethelinux

NOTE: Use of n and N keys to search the next and previous search pattern you have metioned.
How to search word having space.

Word to find: the tastethelinux

Example:
/the\ tastethelinux

NOTE: Use of n and N keys to search the next and previous search pattern you have metioned.
How to search path in a file.

Path to find: /home/tla

Example:
/\/home/tla

NOTE: Use of n and N keys to search the next and previous search pattern you have metioned.
How to search timestamp in a file.

Timestamp to find: May 18 10:10:01

Example:
/May\ 18\ 10:01:01

NOTE: Use of n and N keys to search the next and previous search pattern you have metioned.
How to Examine File using less command in Linux.

press E(shift+e) this will prompt to the screen for ":Examine:" and then enter the file name first_file and press enter key.

press :n to go at next file and :p to go at previous file.
How to edit the file using less command.

press v to edit and it will open a nano editor for you.

[wp_quiz id=”2735″]

Give your valuable time