[Linux] How to use the more command, unexpected uses [File concatenation]

Hello everyone.
I work in the System Solutions Department and I often find myself struggling to decide whether to buy new or used home appliances.

Now, when it comes to commands for viewing files in a Linux environment,
there are "more," we will explain today

Those who are unfamiliar with
the "more" command and "How is it different from other commands?" Those who are familiar with the command may be wondering, "I never use more," or "Compared to other commands, it's not that great..."

While explaining the basic usage of "more," I will also introduce my views and suggestions on such topics as "why its usage rate is low" and "when to use more."

To give you the conclusion first, it is useful when concatenating files using specifications rather than for everyday use

test environment

  • Linux environment
    “OS: Ubuntu 20.04.5 LTS (WSL2 environment)”

・Shell: Bash
・Change locale to Japanese

How to use the more command

more [options] file name *Options can be omitted

To use it, just specify the file name (relative or absolute path is possible) just like with a general editor or viewer

When you run it, "the content that fits within the size of the screen (console) will be displayed."

more and less are functions called "pagers" that display file information in a fixed number of lines

Main options

+Number Display from the line with the specified number
-l Ignore page breaks.
Normally, more will stop at the next line when it encounters a ^L (line break), but this disables that feature.
-f Show data lines instead of screen lines

If there is a long line, it will wrap on the screen and be displayed one line at a time, but multiple lines that have been wrapped on the screen will be displayed simultaneously as one complete line

-s Displays consecutive blank lines as one line
-d When an invalid key is pressed during operation, a help message about the operation will be displayed instead of a beep
-Number Displays the specified number of lines at a time

* Check the option explanation and actual behavior of more using the man command.
Only commonly used options are listed.

Runtime behavior

■ When the file contents fit on the screen at one time

$ more testfile test text *A sample file with only one line "test text"

If the number of lines fits in the console, they are simply output as is the command is immediately

If it can be displayed in the console, there is no difference between this and using the cat command without any options

■ When the file contents do not fit on the screen at one time (general usage)

$ more testfile_long 1 2 3 4 5 6 7 8 9 10 --Continued--(40%) *Sample file with numbers listed in order from 1

If the number of lines does not fit within the console's display area, only the lines that fit will be displayed in their entirety

more is an interactive state where the above state is running and subcommands are accepted

In interactive situations, it behaves similarly to less and view

Pressing Enter will display the undisplayed part line by line, and pressing Space will display the next line per screen size. Once all the lines have been displayed, the process is complete and the system will return to the state before execution, where the "$" (prompt) is displayed.

Operation method (main interactive commands)

"k" is any decimal number

For example, if you press "z", the display will advance by the number of lines on the screen, but if you enter "1z", it will "display one line ahead"

h,? Help function.
Displays a list of subcommands and their explanations.
SPACE Display k lines ahead,
defaults to the current number of lines on the screen.
z k Display lines.
Default is the current number of lines on the screen.
The argument becomes the new default.
RETURN (Enter) k Show destinations. Default is 1. Argument becomes new default
s Skip k lines ahead (this is not scrolling, so skip to the top).
Default is 1.
f Scrolls the screen k times (this is in screen units, so the entire display will change).
The default is 1.
b Scrolls the screen back k times (this is in screen units, so the entire display will change).
Default is 1.
= Displays the current line number
/pattern Searches for the kth occurrence of the regular expression.
Default is 1.
q Finish

The more command is difficult to use these days

There is a misconception, or rather a little-known fact, that more can restore the display.

Currently, you can move backwards with the subcommand "b." (Just to clarify)

However, immediately after its implementation, the specification was that "the display would only move forward and could not go back," so it seems that this information and misunderstanding still remains today

You can also search using "/", and it has all the necessary functions

...However, there are other specifications that cause some concerns

Problems

・"Operation ends when all is displayed"

Even if you watch the content to the end and then try to watch the content at the beginning, the operation will end

・"If it's all displayed at once, you can't operate it, so it's no different from cat."

This specification is inconvenient when you want to search while displaying a quick image

・"I can't display the entire line number"

There is no option to display line numbers in parallel across the board like "cat -n" or "less -N"

Given these characteristics, it must be said that there is little reason to use "more" as a command in everyday use.

I think this is why people generally conclude that "less or view are fine."

Tip: "less" was originally a later command created by improving "more"

"Less" was developed later with the intention of improving on the initial "more" command, which only advanced the display and did not go back . (From the command creator's website)

Let's take a look at the less man page:

DESCRIPTION
less is a program similar to more(1), but allows you to move backward as well as forward within a file.

Also, less does not need to read the entire input file at startup, so it starts up faster than text editors like vi(1) for large input files.
Less uses termcap (or terminfo on some systems), so it can run on many terminals.
Support for hardcopy terminals is still limited (on hardcopy terminals, lines that should be displayed at the top of the screen are marked with a caret (^)).

The commands are based on both more and vi. Some commands can be used after a decimal number (represented by N in the following descriptions)

" less is a program similar to more(1) but allows you to move backward as well as forward through a file. "

It clearly states that "backward movement" is based on the assumption of "more" (which could not be used to move backward)

more (1978)

man: Command created in 1978 *See references
less: Command created in 1983 *See references

In 1978, before Linux, in the days of UNIX, the "more" command was created to improve the status quo at the time

It is a pager (command) officially adopted by operating systems including UNIX

In this article, we will pay respect to the history and historical existence of less, but will also write with the understanding that "less is more convenient and widespread in modern times."

Useful specifications of the more command

"More" has some interesting features that "less" and "view" don't have

When opening multiple files, the file names are also output to standard output

When you open multiple files using less or view, the display screen will switch for each file

For example, let's open two files, testA.conf and testB.conf, which contain permutations of numbers from 1 to 10, at the same time

■ When opened simultaneously with less

$ less testA.conf testB.conf 1 2 3 4 5 testA.conf (file 1 of 2) (END) - Next: testB.conf *Displayed in the command line

:n to move to the next file

6 7 8 9 10 testB.conf (file 2 of 2) (END) *Displayed in the command line

In less, the information is simply output to the command line.
* In view, the format is the same, just the wording is different.

■ When opened simultaneously with more

$ more testA.conf testB.conf :::::::::::::: testA.conf :::::::::::::: 1 2 3 4 5 --Continued--(Next file: testB.conf) *Displayed in the command line 

Use subcommands (Enter or Space) to advance the display

:::::::::::::: testA.conf :::::::::::::: 1 2 3 4 5 :::::::::::::: testB.conf :::::::::::::: 6 7 8 9 10

more includes the filename in the output and handles it differently

You may be wondering, "What's so good about that?", but this specification leads to the next "use."

When to use the more command

This may not be the case normally, but there are rare cases where you want to combine multiple confs into a single file

- For management, documentation, and archiving purposes, I want to keep it in one file that can be searched all at once and is easy to save, rather than individual conf files

I have only had one experience in the past where the above situation occurred

This is when the above specifications of "more" shine

■ When concatenated with "more"

$ more testA.conf testB.conf > testA+C.conf $ cat testA+C.conf :::::::::::::: testA.conf :::::::::::::: 1 2 3 4 5 :::::::::::::: testB.conf :::::::::::::: 6 7 8 9 10

Since the file name is output as standard, you can include the file name when concatenating

Conversely, try concatenating files using less:

■ When concatenated with less

$ less testA.conf testB.conf > testA+C_less.conf $ cat testA+C_less.conf 1 2 3 4 5 6 7 8 9 10 

"Only the contents are linked, and it is not possible to see where the files are connected," which does not achieve the purpose

Conclusion: I don't usually use it, but it's useful when you want to concatenate files including file names

In conclusion, "less" is convenient and easy to use for everyday use

However, "more" shines when you want to include the file name in the concatenation

On the other hand, if you don't use "more"

$ for file in testA.conf testB.conf; do echo "==== $file ====" >> combined.conf; cat $file >> combined.conf; done $ cat combined.conf ==== testA.conf ==== 1 2 3 4 5 ==== testB.conf ==== 6 7 8 9 10

It's a pain. I think typing "more" is definitely easier than typing this

"More" is especially easy to use when combined with find, so I don't think there's any point in not using it

lastly

The reason I am writing this blog is because I have struggled in the past with concatenations that include file names

Among them, I was very impressed when I was able to achieve a beautiful result using the more specifications.
It was a great learning experience for me: "If you understand the specifications, sometimes you can solve things easily."

I hope this article provides some useful knowledge to those who read it.
Thank you for reading this far.

Reference materials

man ”more” page (more version 5.19 information)
-man-db/focal,now 2.9.1-1 amd64

The early history of the more command ※Developer's website
https://danhalbert.org/more.html

Less *Developer's website:
https://www.greenwoodsoftware.com/less/index.html

more(1) - Arch manual pages
https://man.archlinux.org/man/more.1.en

If you found this article helpful , please give it a like!
7
Loading...
7 votes, average: 1.00 / 17
10,493
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author

inside

I joined Beyond mid-career and
working in the System Solutions Department.
I have LPIC-3 304 and AWS SAA certifications.