A Tale of “More” and “Less” Commands in Linux

Linux TLDR
Last Updated:
Reading time: 5 minutes

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

DescriptionMore & Less Commands
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesmore, less
Internet RequiredNo

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:

CommandsDescription
EnterMove forward one line.
Space bar | Up arrowMove forward one page.
B | Down arrowMove backward one page.
/patternSearch forward for matching patterns.
=Print the current line number.
vOpen the specified file in the “/usr/bin/vi” editor or program.
!Execute the commands in the sub-shell.
.Repeat the previously executed command.
hOpen the help section.
qQuit 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:

more command

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:

Opening the file from a specific line

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:

Printing the first five lines from the file

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:

Displaying help instead of ringing the bell

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:

Reading the logical rather than screen lines

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:

Removing the extra blank lines from the file

Omitting the Bold and Underlines

The “-u” flag will suppress the string if it contains bold or underlines.

$ more -u article.txt

Output:

Omitting the bold and underlines

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:

OptionsDescription
-EAutomatically quit the file once it reaches the end, behaving like more command.
-fForce the non-regular files to open.
-FIf the content size fits the screen size, then close the interactive screen.
-gOnly highlight the strings that match the last search.
-GDon’t highlight any string matches from the last search.
-iIgnore the case in searches that do not contain uppercase.
-IIgnore case in all searches.
-nSuppress the line number.
-pStart the file with the first occurrence of the pattern.
-sSqueeze the multiple blank lines into one.

Also, check the following key maps that come into use while viewing the file:

CommandAction
Down arrow, Enter, e, or jMove one line forward.
Up arrow, y, or kMove one line backward.
Space bar or fMove one page forward.
bMove one page backward.
/patternSearch forward for matching patterns.
?patternSearch backward for matching patterns.
nRepeat the previous search.
NRepeat previous searches in a backward direction.
gGo to the first line in the file.
NgGo to the nth line in the file.
GGo to the last line in the file.
pGo to the beginning of the file.
NpGo to line N percent into the file.
vEdit the current file with $VISUAL or $EDITOR.
!Execute the commands in the sub-shell.
hDisplay help.
qQuit 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:

less command

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:

Highlighting all the matched occurrence in the file

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:

Searching for exact match in the file

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:

Showing output along with line numbers

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:

Automatically close the file if the content fits with screen size

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:

Leaving the content on screen

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:

Monitor file changes in less command

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.