1. Overview
When viewing file contents in Linux, we may benefit from some interactive features to help us. We might need to see statistics of a file that we're viewing, mark a certain line then return to it, or view the contents of a file while it's being updated.
In this tutorial, we'll briefly discuss the usage and differences of three terminal pagers that are used in Linux.
Terminal pagers are used to view files page by page, and/or line by line. We're going to explore three of them: more, less, and most. All of them have similar features such as viewing multiple files simultaneously, but each one has a prominent feature or advantage that might make us consider using it.
2. Usage
We can use all of the tools by passing one or more file names:
more file1.txt file2 file3 less file1.txt file2 file3 most file1.txt file2 file3
To exit any of the tools, we can press q or ctrl+c.
We can also pipe the output of another command as an input:
history | less
3. Availability
The more tool is available on most Linux and Unix-like operating systems. less is also widely available, but some Alpine Linux distributions don't have it installed by default. On the other hand, most is usually not installed by default.
We can install most using a package manager. For example, on Ubuntu, we can install it using:
apt install most
4. more
4.1. Using more
more is one of the oldest terminal pagers in the UNIX ecosystem. Originally, more could only scroll down, but now we can use it to scroll up one screen-full at a time, and scroll down either one line or one screen-full:
more filename.txt
An example output would be:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sit amet --More--(1%)
On its status bar, more shows the percentage of the file read. It automatically closes when it reaches the end of the file without having to press a button.
4.2. Interactive Commands
more has many interactive commands we can invoke by keystrokes:
- space – go to the next page in accordance with the terminal's size
- b – go back one page
- enter – scroll down one line
- = – display the current line number
- :v – start up the vi text editor at the current line
5. less
One of the reasons why less was introduced was to allow backward movement line by line. It has a lot of commands that are similar to the vi text editor's commands, and it supports horizontal scrolling, live monitoring, and more.
5.1. Support of File Formats
less has support for different file formats. For example, if we try to read a png, jpeg, or jpg file with more, it would just print its binary data, whereas less would print its metadata:
less picture.jpg picture.jpg JPG 743x533 743x533+0+0 8-bit sRGB 45.6KB
less supports other file formats such as jar, war, zip, pdf, tar.gz, gif, png, tiff, tif, and rar.
5.2. Marking
While reading a large file, we may want to set bookmarks in certain locations to be able to return to them.
With less, we can mark a certain line by pressing m followed by another character, for example, A. We can mark another line by pressing m followed by an another character such as B. Then, after we have scrolled elsewhere in the file, we can return to the marked lines by pressing the apostrophe key (‘) followed by the character that we used for marking.
In this example, we can switch between our bookmarks by typing ‘A and ‘B.
5.3. Monitoring
Let's say we want to watch the content of a log file while it's being updated, but we don't want to have to re-run less on it over and over. We can switch to seeing the updating content of the file by pressing Shift+F keys, or by executing the command with the addition of the +F flag:
less +F /var/log/syslog
6. most
most allows us to view multiple files simultaneously and switch between them. It's very useful for viewing large data sets because most does not wrap lines that have more characters than the terminal page. Instead, it truncates them and offers column-by-column horizontal scrolling.
6.1. Multiple Windows
We can display multiple files and switch between them by passing files as arguments to most:
most text.txt file.txt -- MOST: text.txt (18,4) 5%
By default, while reading a file, the status bar displays the file name, the percentage that we've viewed so far, the current line number, and current position horizontally since we can scroll left and right by pressing left and right keys.
We can switch between files by pressing :n. Then, we can use up/down arrow keys to change file names, and press enter to switch to the selected file:
-- MOST: text.txt (18,4) 5% Next File (1): file.txt
We may also want to read one file in binary mode and another file in non-binary mode. most allows us to view different files in different modes. For example, we can toggle options while viewing a file by pressing : followed by o, then we can toggle the binary mode by pressing b.
7. Comparing the Tools
If we want a simple terminal pager that is widely available, then we would choose more.
But, if we want to use vi text editor's commands, and prefer a tool that has more sophisticated scrolling both horizontally and vertically, then less is a good option.
more and less have the option to view multiple files at once. more allows us to view them as a single file separated by lines, and less allows us to switch between them. However, both more and less display all the opened files with the same options.
Finally, if we want to open multiple files with different options, or see more information on the status bar while viewing a file, then most is a good choice, but may require installing in our Linux environment.
8. Conclusion
In this article, we learned about the features of the most common Linux terminal pagers — more, less, and most.
Then we looked at how the tools differ and how to choose between them.