More or Less
more
and less
are both commands used for viewing the content of files or command output in a paginated manner. They have similar purposes, but less
is often referred to as a “better more” due to its enhanced set of features.
Here are the primary differences between the two:
Behaviour | more | less |
---|---|---|
Origin | Older command, hence available on very old Unix systems. | Developed later to address the limitations of more. In modern systems, less is often preferred over more because of its extended functionalities. |
General | Basic viewing capabilities. | Over time, less has been equipped with many more features than more, such as the ability to view multiple files, show line numbers, and horizontal scrolling. |
Navigation | Primarily allows forward navigation through a file or output, one screen/page at a time. You can move to the next page with the spacebar but can’t easily navigate backwards (except in some implementations where you can go back one screen with b). | Provides bidirectional navigation. You can move forwards and backwards, search for patterns, and has more navigation commands akin to editors like vi. With less, you can move forwards with the spacebar, move backwards with b, and scroll line by line with j and k. |
File Handling | Reads the file or command output sequentially. It cannot backtrack in files that are piped through it. | Random access allows less to move freely through the file, even when viewing large files. It can also move backward in piped input, although this might require caching the data, which can use more memory. |
Search Capability | Limited search capabilities. | Offers robust search functionalities. You can search forward with /pattern and backward with ?pattern, among other features. |
Performance | Since it reads files sequentially, more might be faster for simply scrolling through very large files without the need to backtrack. | Uses random file access, which might be slower for some very large files, but offers much more flexibility in terms of navigation. |
When it comes to file access in computing, there are typically two main methods: sequential access and random (or direct) access.
Sequential Access
In Sequential Access, data is read in order, one data block after another, much like how a tape reader works. To get to the middle of the data, you’d have to read through everything before it.
The more
command uses sequential access. When you pipe content or open a file with more, it starts reading the content from the beginning and continues in order. If you’re only going to read from the start to somewhere in the middle of a large file, this can be very efficient because no time is wasted reading or seeking other parts of the file.
Analogy: Imagine you’re listening to songs on a cassette tape and you want to get to song 5. You’d have to fast forward through songs 1-4 to get there.
Random (Direct) Access
In Random Accesss, data can be accessed directly, no matter where it is. You can jump to any part of the data without reading through the entirety of it, much like how you can click on any point in a video’s timeline to start watching from there.
The less
command employs random file access. This means it doesn’t just read a file from start to finish. Instead, it can jump to different parts of the file as needed. This capability is what allows less to offer functionalities like backward navigation, jumping to a specific line number, or quickly searching for patterns anywhere in the file.
However, for very large files, constantly jumping around might introduce tiny delays (due to disk seek times) compared to the smooth sequential read of more.
Analogy: Imagine you’re watching a video on a digital platform. You can click on the 30-minute mark to start watching from there without having to watch or fast forward through the first 30 minutes.
Conclusion
In practical terms, for everyday use and with modern hardware, the difference in speed
between more and less is usually negligible for most files. The power of less
comes from its flexibility and its rich set of features.
However, in specialised scenarios, like processing very large logs or datasets where you’re sure you only need a sequential read without backtracking, more
might be slightly faster due to its simplicity and sequential nature.