[Linux] How to use the more command, unexpected uses [File concatenation]
table of contents
- 1 test environment
- 2 How to use/operate the more command
- 3 The more command is ``difficult to use'' in modern times
- 4 Useful specifications of the more command
- 5 When to use the more command
- 6 Conclusion: I don't usually use it, but it's useful when you want to concatenate files that include file names.
- 7 lastly
Hello everyone.
I work in the System Solutions Department, and people tend to have a lot of trouble deciding whether to buy new or used home appliances.
Now, speaking of commands for viewing files in a Linux environment, there
are "more" which I will explain this time
If you don't know
the "more" command And if you are familiar with it, you might be thinking, "I don't use more," or "It's not that great compared to other commands..." There are probably many.
While explaining the ``basic usage of more'' , I will also introduce my views and suggestions regarding ` `why the usage rate is low'' and ``where to use more''
Let me state the conclusion first: It is useful when concatenating files using specifications rather than everyday use.
test environment
- Linux environment
“OS: Ubuntu 20.04.5 LTS (WSL2 environment)”
・Shell: Bash
・Change locale to Japanese
How to use/operate the more command
more [Options] File name *Options can be omitted
To use it, just specify the file name (relative or absolute path is also possible), just like a general editor or viewer.
When executed, "the contents that fit within the screen (console) size 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 row with the specified number. |
-l | Display ignoring page breaks. More usually stops at the next line when there is a ^L (line feed), but this function is disabled. |
-f | Display the number of rows on the data rather than the number of rows on the screen.
If there is a long line, it will be wrapped on the screen and displayed one line at a time, but multiple lines that have been wrapped on the screen will be displayed simultaneously as a single line. |
-s | Displays consecutive blank lines as one line. |
-d | When an invalid key is entered during operation, a help text regarding the operation method will be displayed instead of a beep sound. |
-Number | Displays the number of rows containing the specified number. |
* Check the option explanation and actual behavior of more confirmed using the man command.
Only commonly used options are listed.
Runtime behavior
■ If the file contents fit on the screen when displayed at once
$ more testfile test text *Sample file containing only one line of "test text"
If the number of lines fits within the console, it is simply output as is the command completes execution immediately .
If it can be displayed in the console, there is no difference from using the cat command without options.
■ When the file contents do not fit on the screen when displayed at once (general usage condition)
$ more testfile_long 1 2 3 4 5 6 7 8 9 10 --continued--(40%) *Sample file where only numbers are listed in order from 1
If the number of lines does not fit within the console's display area, only the number of lines that fit will be displayed.
more is an interactive state where the above state is running and accepts subcommands.
In an interactive state, it behaves similar to less and view.
Enter displays the undisplayed parts line by line, and Space displays the next line for each screen size, but when all is displayed, it is completed and returns to the state before execution where "$" (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 at once, but if you enter "1z", it will "display one line ahead".
h,? | Help function. Display a list and explanation of subcommands. |
SPACE | Display k destination. The default is the current number of screen lines. |
z | k Show destination. The default is the current number of screen lines. The argument becomes the new default. |
RETURN (Enter) | k Show destination. Default is 1. The argument becomes the new default. |
s | k Skip ahead. (This is not scrolling, so the skip destination is the top.) Default is 1. |
f | Scroll the screen k times. (Since it is per screen, all the displays change.) Default is 1. |
b | Scroll back the screen k times. (Since it is per screen, all the displays change.) Default is 1. |
= | Display the current line number (line number). |
/pattern | Search for the kth occurrence of a regular expression. Default is 1. |
q | Finish. |
The more command is ``difficult to use'' in modern times
There is a misconception, or rather a little known fact, that more can be used to restore the display.
Currently, backward movement is possible with the subcommand "b". (Reminder)
However, in the era immediately after its implementation, the specification was such that the display only advanced and could not go back, so it seems that information and misunderstandings still remain.
You can also search using "/", and all the necessary functions are available.
...However, there are some issues with other specifications.
Troubles
・"The operation ends when all are displayed"
After viewing the content until the end, even if you try to view the content at the beginning, the operation is finished.
・"It's no different from cat because it can't be operated if it can't be displayed all at once."
This specification is inconvenient when you want to search while displaying lightly.
・"Line numbers cannot be displayed in their entirety"
There is no option to display line numbers in parallel like "cat -n" or "less -N".
Due to these characteristics, I would have to say that there is little reason to use "more" as a command that you use regularly.
I think this is why people generally come to the conclusion that "less" or "view" would be better.
Tips: In the first place, “less” is a later command that was created as an improvement on “more”
"Less" was created later with the intention of improving the initial "more" command, which "only advanced the display but did not go back . (From the command creator's website)
Let's take a look at the less manual page.
[Description]
less is a program similar to more (1), but it allows backward movement as well as forward movement within a file.Less also starts faster than text editors like vi (1) for large input files, since it does not need to read the entire input file at startup.
Because less uses termcap (or terminfo on some systems), it can run on many terminals.
Support for hardcopy terminals is still limited. (On hardcopy terminals, the line that should appear at the top of the screen is marked with a caret (^))The commands are based on both more and vi. There are also commands that can be used following a decimal number (represented by N in the following explanations).
`` less is a program similar to more(1), but it allows you to move backwards as well as forwards within a file. ''
It is clearly stated about "backwards movement" assuming more (that could not move backwards).
more (1978)
man: Command created in 1978 * See reference materials
less: Command created in 1983 * See reference materials
In 1978, before Linux, during the UNIX era, the "more" command was created to improve the current situation at the time.
It is a pager (command) that has been officially adopted by OSes including UNIX.
In this article, while paying respect to the background and historical existence, I have written with the understanding that "Less is highly convenient and popular in modern times."
Useful specifications of the more command
"More" has some interesting specifications that "less" and "view" do not have.
When opening multiple files, the file names are also output to standard output.
When you open multiple files with less or view, the display screen will change depending on the file.
For example, let's simultaneously open two files, testA.conf and testB.conf, containing permutations of numbers from 1 to 10.
■ When opened simultaneously in less
$ less testA.conf testB.conf 1 2 3 4 5 testA.conf (file 1 of 2) (END) - Next: testB.conf *Displayed in command line part
:n to move to next file
6 7 8 9 10 testB.conf (file 2 of 2) (END) *Displayed in the command line part
Less simply prints information to the command line.
*For view, the format is the same, only the wording is different.
■ When opened at the same time with more
$ more testA.conf testB.conf :::::::::::::: testA.conf ::::::::::::::: 1 2 3 4 5 --continued-- (Next file: testB.conf) *Displayed in the command line part
Advance the display with subcommands (Enter or Space).
:::::::::::::: testA.conf :::::::::::::: 1 2 3 4 5 ::::::::::::::: testB.conf :::::::::::::: 6 7 8 9 10
With more, file names are included in the output content, and file names are handled differently.
You may be wondering, "What's good about that?" This specification leads to the next "use".
When to use the more command
Although it may not happen normally, there are rare situations where you want to combine multiple conf files into one file.
・For management, documentation, and storage purposes, I want to keep a single file that can be searched all at once and is easy to save, instead of having individual conf files.
In my past experience, I have been in the above situation only once.
At this time, 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 output, you can include the file name when concatenating.
Conversely, let's concatenate 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
The purpose cannot be achieved because "the files are concatenated only by their contents, so you can't see where the files are connected."
Conclusion: I don't usually use it, but it's useful when you want to concatenate files that include file names.
In conclusion, "less" is convenient and easy to use for everyday use.
However, when you want to concatenate files including file names, "more" is the best option.
Conversely, when not using "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
troublesome. I think "more" is definitely easier than typing this.
Especially when combined with find, "more" is especially easy, so I don't think there's any point in not using it.
lastly
The reason I wrote this blog was because I had a hard time concatenating files that included file names in the past.
I was very impressed when I was able to achieve this beautifully using the specifications of more.
I remember learning a great lesson: ` `If you understand the specifications, there are things that can be easily solved.''
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
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