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!
Click here to learn about touch command.
touch command in Linux with examples
Cheatsheet of less command in Linux