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

table of contents
Hello everyone.
I'm Nakano from 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,
we will explain today"more,"there are
the "more" commandThose unfamiliar with
And those who are familiar with it might think, "I don't really use 'more'," or "It's not that great compared to other commands."
the basic usage of "more,"This article explainsand"why its usage rate is low" and "when to use more."then presents the author's views and suggestions regarding
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 | This ignores page breaks and displays the text. Normally, `more` stops at the next line when a newline character (^L) is present, but this disables that function. |
| -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 |
* The options and actual behavior of `more` were confirmed 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 within the console, they are simply output as isimmediatelythe command executionis
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 displays the unshown portion one line at a time, while pressing Space displays the next line according to the screen size. Onceall content has been displayed, the process is complete and the "$" (prompt) is displayed, returning to the state before execution.
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 and explanation of subcommands. |
| SPACE | Displays the kth row to go. The default is the current number of rows on the screen. |
| z | `k` displays the next row. The default is the current screen row number. The argument becomes the new default. |
| RETURN (Enter) | k Show destinations. Default is 1. Argument becomes new default |
| s | Skip to the next k line. (This is not scrolling, so the destination will be the top.) The default is 1. |
| f | Scroll the screen k times. (This scrolls screen by screen, so the entire display will change.) The default is 1. |
| b | Scroll back through the screen k times. (This scrolls screen by screen, so the entire display will change.) The default is 1. |
| = | Displays the current line number |
| /pattern | Searches for the k-th match in the regular expression. Default is 1. |
| q | Finish |
The more command is difficult to use these days
There's a common misconception, or rather, a lack of awareness, thatthe "more" button can be used to revert to the previous display.
Currently,you can move backward using the subcommand "b". (Just to reiterate)
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,there's little reason to specifically use "more" as a command for 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"
the initial "more" command, which only advanced the display without going backas an improvement toThe "less" command was created later. (From the command creator's website)
Let's take a look at the less man page:
[Explanation]
less is a program similar to more(1), but it can move backward within a file as well as forward.Furthermore, because less does not need to read the entire input file at startup, it starts faster than text editors like vi(1) for large input files.
Because less uses termcap (or terminfo, depending on the system), it can run on many terminals.
However, support for hardcopy terminals is still limited. (On hardcopy terminals, a caret (^) appears on the line that should be displayed at the top of the screen.)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 it can move backward within a file as well as forward."
It clearly states that "backward movement" is based on the assumption of "more" (which could not be used to move backward)
more (1978)
man: A command created in 1978. *See reference materials.
less: A command created in 1983. *See reference materials.
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
With `less`, the information is simply output to the command line.
*Note: `view` uses the same format, just with different wording.
■ 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
I was deeply moved when I was able to beautifully implement it using the `more` specification.
"Sometimes, if you understand the specifications, you can solve problems easily."It's a memorable experience where I learned a great lesson:
I hope this article provides some useful information to those who read it.
Thank you for reading this far.
Reference materials
man “more” page (information as of more version 5.19)
-man-db/focal,now 2.9.1-1 amd64
The early history of the `more` command. (See the 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
8
