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 more command is used to read large files by displaying one page at a time and scrolling up and down through pages; you can even pipe it with other commands like cat.
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.
Tutorial Details
Description | More & Less Commands |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | more, less |
Internet Required | No |
So, this article will be divided into two sections: first, we will start with the “more
” command and then the “less
” command.
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:
Commands | Description |
---|---|
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 “/usr/bin/vi ” editor or program. |
! | 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
Output:
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
Output:
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
Output:
Displaying the Help Menu at the Bottom of the File
The “-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
Output:
Reading the Logical rather than Screen Lines
The “-f
” flag will count the logical lines that are not wrapped by the screen width, showing you the actual lines.
$ more -f article.txt
Output:
Squeezing the Multiple Blank Lines into One
The “-s
” flag will replace the sequence of multiple blank lines with one single line, making output cleaner.
$ more -s article.txt
Output:
Omitting the Bold and Underlines
The “-u
” flag will suppress the string if it contains bold or underlines.
$ more -u article.txt
Output:
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:
Options | Description |
---|---|
-E | Automatically quit the file once it reaches the end, behaving like more command. |
-f | Force the non-regular files to open. |
-F | If the content size fits the screen size, then close the interactive screen. |
-g | Only highlight the strings that match the last search. |
-G | Don’t highlight any string matches from the last search. |
-i | Ignore the case in searches that do not contain uppercase. |
-I | Ignore case in all searches. |
-n | Suppress the line number. |
-p | Start the file with the first occurrence of the pattern. |
-s | Squeeze the multiple blank lines into one. |
Also, check the following key maps that come into use while viewing the file:
Command | Action |
---|---|
Down arrow , Enter , e , or j | Move one line forward. |
Up arrow , y , or k | Move one line backward. |
Space bar or f | Move one page forward. |
b | Move one page backward. |
/pattern | Search forward for matching patterns. |
?pattern | Search backward for matching patterns. |
n | Repeat the previous search. |
N | Repeat previous searches in a backward direction. |
g | Go to the first line in the file. |
Ng | Go to the nth line in the file. |
G | Go to the last line in the file. |
p | Go to the beginning of the file. |
Np | Go to line N percent into the file. |
v | Edit the current file with $VISUAL or $EDITOR. |
! | Execute the commands in the sub-shell. |
h | Display help. |
q | 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
Output:
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
Output:
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
Output:
Printing the Line Number
The “-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
Output:
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
Output:
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
Output:
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 “+F
” flag.
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
Output:
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.
Peace!!
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.