This is the third day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

This is the 44th article in the Linux Minimalist Trivia series, which introduces the real and useful Linux knowledge. Check it out!


More and less

  • “More” is used to view files in separate screens. For files with too much content, you can display the content screen by screen (page by page). Use Spaces to advance to the next page; Pressing B will show you a page back. When the more command starts, it loads the entire file, reading from front to back.

  • Less indicates less is more, which is an enhancement of the more command. When using the more command to view content, you can only use the scroll wheel to view the content and use the B key to go back to the page. Less adds the up and down arrow keys to display the up and down contents on the current screen.

There is a saying in Linux: “Less is more”.

Command for viewing content in more pages

In addition to viewing file contents by page, more supports line hopping and search.

The command parameter

  • +n displays from the NTH line

  • -n Sets the screen size to N lines

  • +/ PATTERN searches for the pattern before each file display, and then displays the pattern starting after the first two lines of the string

  • -c Clear the screen from the top and display it

  • -d Press space to continue, ‘q’ to quit (Press the space key to continue, and Press the Q key to exit) is displayed to disable the ring tone function

  • -l Ignores the Ctrl+ L (page feed) character

  • -p repages a file by clearing the window rather than scrolling, similar to the -c option

  • -s Displays consecutive blank lines as one line

  • -u Deletes the underlined line in the file

Common operation commands or buttons

  • Enter goes down n rows and needs to be defined. The default is 1 line

  • Ctrl + F scroll down one screen

  • Space bar scroll down one screen

  • Ctrl + B returns to previous screen

  • = Prints the line number of the current line

  • : + f Outputs the file name and the line number of the current line

  • V Calls the vi editor (lowercase v key)

  • ! The command invokes the Shell and executes the command.

  • Q out more

Using the more command

  • View the contents of the first screen of my.txt.
$ more my.txt line-1 line-2 line-3 ...... Omitted lillne-21 line-22 line-23 --More--(5%)Copy the code
  • Displays the content starting at line 6
$ more +6 my.txt
line-6
line-7
line-8
line-9
......
Copy the code
  • Set the display to 5 lines per page
$ more -5 my.txt
line-1
line-2
line-3
line-4
line-5
--More--(1%)
Copy the code
  • Looks for the first line in the file where the string “10” appears and displays the output starting with the first two lines.
$ more +/10 my.txt ... skipping line-8 line-9 line-10 line-11 line-12 line-13 ......Copy the code
  • More accepts pipe input to display content
$ cat my.txt | more -5
line-1
line-2
line-3
line-4
line-5
--More--
Copy the code

Less instruction

Less is also used to display file contents in pages. But the use of less is more powerful and flexible and elastic than that of more.

Less can use the up and down arrow keys ([PageUp] [PageDown]) to scroll through files and search down and up.

Less does not load the entire file when viewing.

Option Parameter or command

Option to show

-b < buffer size > Sets the size of the buffer

-e Automatically leaves when the file is displayed

-f forces the opening of special files, such as peripheral codes, directories, and binaries

-g indicates only the last keyword searched

-i Ignores the case of the search

-m Displays the percentage of more commands

-n Displays the line number of each line

-o < filename > saves the output of less in the specified file

-q Does not use the warning tone

-s Displays a consecutive empty row

-s If the line is too long, the exceeding part will be discarded

-x < digit > Displays the TAB key as a specified digit space

View the operation instructions in the process

/ String: Searches down for string

? String: The ability to search up for string

N: Repeat the previous search (and/or? The relevant)

N: Repeat the previous search in reverse (and/or? The relevant)

B Turn back a page

D Turn back half a page

H The help page is displayed

Q Exits the less command

U Scroll forward half a page

Y scroll forward one row

The space bar scrolls a line

Enter to scroll a page

[pageDown] : Scroll down a page

[PageUp] : Scroll up a page

V Calls the vi editor (lowercase v key)

&pattern – Displays only lines that match the pattern, not the entire file

Less opens the file, enters &, changes to &/, and enters the matching content. Press Enter to display all matching lines

For example, &/10 Press Enter. The following information is displayed:

line-10
line-100
line-101
line-102
line-103
line-104
line-105
line-106
line-107
line-108
line-109
line-110
line-210
line-310
~
~
~
~
~
~
~
~
~
& (END)
Copy the code

Other operation commands and buttons

  1. Full screen navigation

CTRL + F – Move one screen forward

CTRL + B – Move back one screen

CTRL + D – Move half screen forward

CTRL + U – Move back half screen

  1. Single navigation

J – Move forward one row

K – Move back one row

  1. Other navigation

G – moves to the last row

G – move to the first row

Q/ZZ – Exits the less command

To find the

Press/and Enter the desired string, then press Enter. Press N (next) to continue searching. Press q(quit) or ZZ to leave.

Less Example

  • Ps Displays process information and line numbers in less pages
$ ps -ef|less -N 1 UID PID PPID C STIME TTY TIME CMD 2 root 1 0 0 Oct07 ? 00:02:55 /usr/lib/systemd/systemd 2 --switched-root --system --deserialize 22 3 root 2 0 0 Oct07 ? 00:00:00 [kthreadd] ...... Omitted 16 root 18 2 0 Oct07? 00:00:00 [bioset] 17 root 19 2 0 Oct07 ? 00:00:00 [bioset] 18 root 20 2 0 Oct07 ? 00:00:00 [bioset] :Copy the code
  • Browsing multiple files
$ less my.txt my1.txt line-1 line-2 line-3 ...... Lillne-21 line-22 line-23 my.txt (file 1 of 2)Copy the code

At this time:

Enter :n to switch to the next file, my1.txt.

After entering :p, switch to the previous file, my.txt.

  • This can also be used when browsing a file:eCommand to open another file.
$ less my.txt
......
line-22
line-23
:e
Copy the code

After entering :e, it changes to Examine: :

line-22
line-23
Examine: my1.txt
Copy the code

Press Enter to view my1.txt.

The difference between more and less

To summarize the differences between more and less:

  1. Less can use the up and down arrow keys to display up and down content. More cannot use the up and down arrow keys to control display.
  2. Less does not need to read the entire file and loads the file faster than more.
  3. Less exits and the shell does not leave what was just displayed, while more exits and the shell leaves what was just displayed

reference

Mainly reference from Linux more and less command usage and other materials, combined with the actual test and use.