How to Use the less, more, and most Commands to Read Text Files in Linux
There are many GUI text editors available on a Linux system to view and modify text files. But you might just want to read your text files within the terminal. There are many commands available on Linux that allow you to do that, three of which are less, more, and most.
Read on to discover how you can use these three commands to effectively read text files on your Linux terminal.
What Is the less Command?
less is a Linux command used for filtering and viewing text files one screen page at a time. It is more advanced than the more and most commands.
With the less command, you can read really long text files in segments without having to load the whole file. It offers a lot of options and interactive features to make your experience more satisfying.
Because the less command outputs the first page of the text file and does not need to read the whole content of the file, it is faster than other text editors.
The less Command Syntax
The basic syntax of the less command is:
less [option] [name or location of the file]
How to Use the less Command
For this example, we will be using the sudo.conf file. The sudo.conf file is used to configure the sudo front end, and it has 139 lines. This file is present in almost every Linux-based operating system. You can also use any text file of your choice—as long as it has over 60 lines.
Using the Default less Command
The default less command prints out the first page of the file you use. Try it out with the sudo.conf file by executing this command in your terminal:
This prints out the first 53 lines of the document. To move forward a line at a time, press the Down key or Space.
To move backward by a line, press the Up key.
To move forward by a page, press B. To move forward several lines, hit B, then type the number of lines.
To move backward by a page, press D. To move backward by a number of lines, type D, then the number of lines you want to go back by.
Show Line Numbers With the less Command
You can also see the number of lines the file has as you view it. To do this, add the -N option when running the command. Try it out with the sudo.conf file by executing this command:
less -N /etc/sudo.conf
Search for Text Using the less Command
You can search for words and strings using the less command. When it finds the string, it will highlight the results in yellow.
Let’s search for the word: plugin. To do this, execute the default less command, and when it returns the output, type / and search for the word or string.
If you search for plugin, the output should look like this:
What Is the more Command?
The more command lets you view text files in your terminal one screen page at a time. This command works just like the less command but only with fewer functionalities.
The more Command Syntax
The basic syntax of the more command is:
more [option] [name or location of the file]
How to Use the more Command
The more command helps a user view portions of a large text file screen by screen in the terminal. You can use it to display text files and command outputs, search for a word in a file, and more.
Using the Default more Command
For this example, we will use the sudo.conf file found in the /etc folder. To use the more command to view a file, execute this command:
The result looks just like that of the less command. There is a difference though, at the bottom left of the screen you will notice that more displays the percentage of the text file, and that number increases or decreases as you move across the file.
Navigating the File Using more
Navigating the file with the more command is similar to the less command. You use the Enter key to move to the next line, D to move to a new page, and B to go back by one page.
Display the First N Lines of a File
Just like the head command in Linux, you can use more to view the first few parts of a file. This is the syntax:
more -N filename
To display the first five lines of the sudo.conf file, execute this command:
more -5 /etc/sudo.conf
What Is the most Command?
Just like the less and more commands, you can use most to read text files on Linux. It is not available in all Linux distributions by default, so you may have to install it yourself.
To check if it is installed, type most into your terminal. If it is not installed, your system will ask if you want to install it. Enter y to install. Alternatively, you can install the package using the default package manager on your machine.
The most Command Syntax
The basic syntax of the most command is:
most [option] [name or location of the file]
To get command-line help regarding the most command, check its manual page by running:
How to Use the most Command
The most command works just like the less and more commands.
The Default most Command
The default most command prints out the first page of the text file. Try it out with:
The output is quite different from the less and more commands. At the bottom, there’s a blue line showing the name of the file and other helpful commands.
Using the less, more, and most Commands With Multiple Files
You can read multiple files at once using all of the above-stated commands. This is the syntax for each:
less filename1 filename2 filename3
more filename1 filename2 filename3
most filename1 filename2 filename3
Using less, more, and most With Other Commands
You can also direct the output of a command or a running process by using the pipe symbol alongside the less/more/most command. The pipe symbol redirects the output of one command as an input to another.
For instance, you could use less when checking the list of running processes on your operating system:
ps aux | less
Learn About Other Text Manipulation Commands on Linux
Just like the less, more, and most commands, Linux offers a lot of commands to help you view, manipulate, and process text files. Some of them include cat, echo, head, and tail. They all fulfill different purposes with their unique features.
Read the full article here