Linux users often use “
more” and “
less” commands interchangeably to read large files without understanding their differences, as their definitions are also identical in the manual.
So, what’s their difference, and when should you use them? All of this doubt will be cleared in this article, including how to effectively use them with practical examples.
But first, let’s start with their definition:
The less command can be considered an extended version that inherits all the more command features and delivers additional features and functionality that the more command lacks.
Like easy navigation and search options, it does not load the complete file but instead accesses it page by page, which optimizes the loading speed while reading large files.
Showing the differences between both of these commands might not make sense because the “
less” command is way more advanced compared to the “
more” command, but if you’re interested in learning more, read our complete article and also check this.
|Description||More & Less Commands|
|Root or Sudo Privileges||No|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
So, this article will be divided into two sections: first, we will start with the “
more” command and then the “
If you are a beginner and just ended up here to learn about a tool to read a large file, I recommend that you skip the more command section and directly jump to the less command section.
Part 1: The “More” Command
The more commands take two arguments: one is the option, and the other is the file name (or complete path pointing towards the file).
$ more [OPTION] [FILE]
The complete options can be checked using the “
more --help” command. As this command is rarely used today, we will just focus on the most relevant examples.
But don’t forget to check the following key maps that will come into use while viewing the file:
|Enter||Move forward one line.|
|Space bar | Up arrow||Move forward one page.|
|B | Down arrow||Move backward one page.|
|/pattern||Search forward for matching patterns.|
|=||Print the current line number.|
|v||Open the specified file in the “|
|!||Execute the commands in the sub-shell.|
|.||Repeat the previously executed command.|
|h||Open the help section.|
|q||Quit the screen.|
Reading a File
You can directly specify the file as an argument to more command or you can pipe it with other commands, as shown.
$ more article.txt #OR $ cat article.txt | more
Directly Open the File at a Specific Line
Open the file from a specific line instead of from the beginning by using the “
+[N]” format and replacing the “
N” with the line number.
$ more +50 article.txt
Printing the First Few Lines
Identical to the previous command, you can specify the number of lines that you just want to print in “
-[N]” format; replace “
N” with the number of lines.
$ more -5 article.txt
Displaying the Help Menu at the Bottom of the File
-d” flag will guide you at the bottom of the file, like “
[Press space to continue, ‘q’ to quit.]” and displays “
[Press ‘h’ for instructions.]” when the wrong key is pressed.
$ more -d article.txt
Reading the Logical rather than Screen Lines
-f” flag will count the logical lines that are not wrapped by the screen width, showing you the actual lines.
$ more -f article.txt
Squeezing the Multiple Blank Lines into One
-s” flag will replace the sequence of multiple blank lines with one single line, making output cleaner.
$ more -s article.txt
Omitting the Bold and Underlines
-u” flag will suppress the string if it contains bold or underlines.
$ more -u article.txt
The rest of the things are not that important, and you might rarely use them, so I did not cover them. So, leaving the more command examples here, let’s move on to the less command examples.
Part 2: The “Less” Command
The less command also accepts two arguments: one is the option, and the other is the filename or path pointing towards a file.
$ less [OPTION] [FILE]
The following are known options that will come into use while using this command:
|Automatically quit the file once it reaches the end, behaving like more command.|
|Force the non-regular files to open.|
|If the content size fits the screen size, then close the interactive screen.|
|Only highlight the strings that match the last search.|
|Don’t highlight any string matches from the last search.|
|Ignore the case in searches that do not contain uppercase.|
|Ignore case in all searches.|
|Suppress the line number.|
|Start the file with the first occurrence of the pattern.|
|Squeeze the multiple blank lines into one.|
Also, check the following key maps that come into use while viewing the file:
|Move one line forward.|
|Move one line backward.|
|Move one page forward.|
|Move one page backward.|
|Search forward for matching patterns.|
|Search backward for matching patterns.|
|Repeat the previous search.|
|Repeat previous searches in a backward direction.|
|Go to the first line in the file.|
|Go to the nth line in the file.|
|Go to the last line in the file.|
|Go to the beginning of the file.|
|Go to line N percent into the file.|
|Edit the current file with $VISUAL or $EDITOR.|
|Execute the commands in the sub-shell.|
|Quit the interactive screen.|
Reading a File
Specify the filename or path pointing towards a file as an argument to the less command; it can also be used with other commands through piping.
$ less article.txt #OR $ cat article.txt | less
Multiple files can be viewed by separating each one of them with a space, and you can easily swim between all the files using the “
:n” (move forward) or “
:p” (move backward) commands.
Displaying All the Matched Occurrences in the File
Use the “
-p” flag with a string to highlight all matching occurrences in the referenced file.
$ less -p "less" article.txt
Displaying the Exact Matched Occurrences in the File
In the previous command, if the specified string comes in between another word or is relative to another word, like “the” for “they“, “them“, “their“, etc. are relative strings, they all get highlighted, and strings with different case like “The” with a capital “T” did not get highlighted.
This all can be easily resolved by using the “
-i” flag, which will ignore all cases, and specifying a space at the end of the string after specifying it inside double quotes to find an exact match.
$ less -i -p "the " article.txt
Printing the Line Number
-N” flag will print the line number at the beginning of each line, which can be very useful while configuring the configuration file.
$ less -N article.txt
End the Interactive Screen if the Content of the File Fits with Screen
If the content properly fits on the screen without cropping, the “
-F” flag will automatically close the interactive screen (or quit the less command).
$ less -F shortfile.txt
Unclear the Screen After Closing the File
By default, when you leave the file using the “
q” key, the content of the file is cleared from the screen; this can be easily prevented using the “
-X” flag; it will leave content on the screen.
$ less -X article.txt
Monitor File Changes
Like the watch and tail commands, less also provides you with the option to monitor file changes in real time using the “
For example, I’ve misconfigured my nginx configuration file, so when the server tries to start, the error will be saved to the “
/var/log/nginx/error.log” file, which will be monitored using the less command.
$ sudo less +F /var/log/nginx/error.log
Alternatively, you can also use the “
Shift+f” shortcut key to enter watching mode, unlike flag.
So, the article ends here.
However, if any useful examples that should be added to this article are left, then please inform us in the comment section.