In this article, we cover how to view contents of a file with less command in Linux. It isn't feasible to open a large enough text file through a GUI application. It would take a bit of time to open. There are times when it takes so long for a file to open that it seems like an eternity. But, there are numerous command-line options like cat, less, more etc. available that can get things done quicker.
We can even view contents of a file with text-editors like vi, nano etc. A user may opt any method as per his/her requirements. One such approach, which we prefer is viewing contents of a file with less command.
And, the reason why prefer less command over others is -
It doesn't reads the entire file at once. So, it responds faster. Apart from that, if we want to search for a specific pattern then we can do that as well. Besides, there are plethora of options available which can be used to our advantage. We will cover more about it Additional Info section.
View contents of a file with less command in Linux
To view contents of a file, use the following syntax -
less [options] <file-name>
For instance, if we want to view
/etc/hosts file -
Press q to quit/exit.
Use Page Up/Page Down keys to scroll back and forward one page. Then, there are Up and Down arrow keys to scroll up and down one line at a time.
G to move at the end of file and g to move at the beginning of file.
To search a pattern - use /patternFor instance, if we are looking for a word - hello. Then, it should be /hello and press Enter. If there are multiple of those then, press n to look for next pattern.
In conclusion, we have covered how to view contents of a file with less command in Linux.
Additional Info -
As we discussed at the beginning of article, there are plethora of options we can use with less command. All such options can be read through man pages -
Apart from that, we can also use help screen by press h key in an opened file. For instance,
less /etc/hosts Now, press h key for more options.