Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the more and less commands of Linux

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to use Linux's more and less commands". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Linux's more and less commands" can help you solve the problem.

The more and less commands are the most frequently used commands in Linux when viewing text, and they are very efficient and simple.

1. More command

The more function is similar to cat, where the cat command displays the contents of the entire file on the screen from top to bottom. More will be a page-by-page display to facilitate users to read page by page, and the most basic instruction is to press the blank key (space) to display to the next page, press the b key to go back to (back) a page to display, and has the function of searching for strings. The more command reads the file back and forth, so the entire file is loaded at startup.

1. Command format:

More [- dlfpcsu] [- num] [+ / pattern] [+ linenum] [file... ]

2. Command function:

The more command is the same as cat's function is to view the contents of the file, but the difference is that more can view the contents of the file by page, but also supports direct jump line and other functions.

3. Command parameters:

+ n starts with line n.

-n defines the screen size as n lines

+ / pattern searches for the string (pattern) before each file is displayed, and then displays it after the first two lines of the string

-c clear the screen from the top and display

-d prompt "Press space to continue,'q' to quit (press Spacebar to continue, press Q key to exit)" to disable the ringing function

-l ignores Ctrl+l (page feed) characters

-p page the file by clearing the window instead of scrolling, similar to the-c option

-s displays consecutive blank lines as one line

-u remove the underline from the contents of the file

4. Common operation commands:

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

Ctrl+F scrolls down one screen

The space bar scrolls down one screen

Ctrl+B returns to the previous screen

= output the line number of the current line

: F output file name and line number of the current line

V calls the vi editor

! Command invokes Shell and executes the command

Q exit more

5. Command example:

Example 1: displays the contents of the file starting from line 3

Command:

Aijian.shi@U-aijian-shi:~/ALM$ cat test.log # displays all log contents aijian.shi@U-aijian-shi:~/ALM$ more + 3 test.log # displays log contents from the third line

Output:

1 aijian.shi@U-aijian-shi:~/ALM$ cat test.log2 2016-8-1 aijian.shi3 2016-8-2 yafang.wei4 2016-8-3 hong.zhan5 2016-8-4 yuyan.zhang6 2016-8-5 senlin.zhao7 2016-8-6 yanbin.liu8 2016-8-7 hui.liu9 2016-8-8-8 yanhua.liu10 2016-8-9 baoixn.cui11 2016-8-10 ge.song12 2016-8-11 zhongjun.zhen13 2016-8-12 qiu.liaoaijian.shi@U-aijian -shi:~/ALM$ more + 3 test.log2016-8-3 hong.zhan2016-8-4 yuyan.zhang2016-8-5 senlin.zhao2016-8-6 yanbin.liu2016-8-7 hui.liu2016-8-8 yanhua.liu2016-8-9 baoixn.cui2016-8-10 ge.song2016-8-11 zhongjun.zhen2016-8-12 qiu.lia

Example 2. Set the content of the log to 4 lines per screen

Command:

Aijian.shi@U-aijian-shi:~/ALM$ more-4 test.log

Output:

1 aijian.shi@U-aijian-shi:~/ALM$ more-4 test.log2 2016-8-1 aijian.shi3 2016-8-2 yafang.wei4 2016-8-3 hong.zhan5 2016-8-4 yuyan.zhang67... skipping one line # here use ctrl+F or the space bar to scroll 8 2016-8-6 yanbin.liu9 2016-8-7 hui.liu10 2016-8-8-8 yanhua.liu11 2016-8-9 baoixn.cui1213... skipping one line14 2016- 8-11 zhongjun.zhen15 2016-8-12 qiu.liao

Example 3. Find the first line in the file where the "liu" string appears, and display the output from the first two lines there

Command:

Aijian.shi@U-aijian-shi:~/ALM$ more + / liu test.log

Output:

1 aijian.shi@U-aijian-shi:~/ALM$ more + / liu test.log23... skipping4 2016-8-4 yuyan.zhang5 2016-8-5 senlin.zhao6 2016-8-6 yanbin.liu7 2016-8-7 hui.liu8 2016-8-8 yanhua.liu9 2016-8-9 baoixn.cui10 2016-8-10 ge.song11 2016-8-11 zhongjun.zhen12 2016-8-12 qiu.liao

Example 4. When there are too many files in a directory, it can be paged and displayed with more. This has to be combined with plumbing |

Command:

Aijian.shi@U-aijian-shi:~/ALM$ cat test.log | more-5 # "|" indicates the pipe. The function is to use the output of the previous command as the input of the following command.

Output:

1 aijian.shi@U-aijian-shi:~/ALM$ cat test.log | more- 52 2016-8-1 aijian.shi3 2016-8-2 yafang.wei4 2016-8-3 hong.zhan5 2016-8-4 yuyan.zhang6 2016-8-5 senlin.zhao78... skipping one line9 2016-8-7 hui.liu10 2016-8-8-8 yanhua.liu11 2016-8-9 baoixn.cui12 2016-8-10 ge.song13 2016-8-11 zhongjun.zhen14-- more-- II, less instruction

The less tool is also a tool for pagination display of files or other output. It should be said that it is a tool for linux orthodox viewing the contents of files, which is extremely powerful. The usage of less is more flexible than that of more. In more, we have no way to turn to the front, we can only look back, but if we use less, we can use the function of [pageup] [pagedown] and other keys to go back and forth to browse the file, which is easier to view the contents of a file! In addition, less can have more search functions, not only to search down, but also to search up.

1. Command format:

Less [parameters] file

2. Command function:

Less is similar to more, but you can browse files at will with less, while more can only move forward, but not backward, and less does not load the entire file before viewing it.

3. Command parameters:

-b sets the size of the buffer

-e automatically leave when the file is displayed

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

-g marks only the last keyword searched

-I ignore the case of the search

-m displays the percentage similar to the more command

-N displays the line number of each line

-o Save the less output in the specified file

-Q does not use warning tones

-s displays a row of continuous empty behavior

-S line will be discarded if it takes too long

-x displays the "tab" key as a specified number space

/ string: the ability to search down for "string"

? Strings: the ability to search up for "strings"

N: repeat the previous search (and / or? Related)

N: reverse repeat the previous search (and / or? Related)

B turn back one page

D turn back half a page

H displays the help interface

Q exits the less command

U scroll forward half a page

Y scroll forward one line

The spacebar scrolls one line

Press enter to scroll one page

[pagedown]: turn a page down

[pageup]: turn a page up

4. Use an example:

The instance 1:ps views the process information and displays the line number through less paging display

Command:

Aijian.shi@U-aijian-shi:~/ALM$ ps-ef | less-N

Output:

1 1 UID PID PPID C STIME TTY TIME CMD2 2 root 1 00 Aug08? 00:00:00 / sbin/init3 3 root 2 00 Aug08? 00:00:00 [kthreadd] 4 4 root 3 2 0 Aug08? 00:00:02 [ksoftirqd/0] 5 5 root 6 2 0 Aug08? 00:00:00 [migration/0] 6 6 root 7 2 0 Aug08? 00:00:00 [watchdog/0] 7 7 root 8 2 0 Aug08? 00:00:00 [migration/1] 8 8 root 9 2 0 Aug08? 00:00:00 [kworker/1:0] 9 9 root 10 2 0 Aug08? 00:00:01 [ksoftirqd/1] 10 10 root 11 2 0 Aug08? 00:00:00 [watchdog/1] 11 11 root 12 2 0 Aug08? 00:00:00 [migration/2] 12 12 root 14 2 0 Aug08? 00:00:01 [ksoftirqd/2] 13 13 root 15 20 Aug08? 00:00:00 [watchdog/2] 14 14 root 16 20 Aug08? 00:00:00 [migration/3] 15 15 root 18 20 Aug08? 00:00:01 [ksoftirqd/3] 16 16 root 19 20 Aug08? 00:00:00 [watchdog/3] 17 17 root 20 20 Aug08? 00:00:00 [migration/4] 18 18 root 22 20 Aug08? 00:00:00 [ksoftirqd/4] 19 19 root 23 20 Aug08? 00:00:00 [watchdog/4] 20 20 root 24 20 Aug08? 00:00:00 [migration/5] 21 21 root 26 20 Aug08? 00:00:00 [ksoftirqd/5] 22 22 root 27 2 0 Aug08? 00:00:00 [watchdog/5] 23 23 root 28 2 0 Aug08? 00:00:00 [migration/6] 24:

Example 2. Browse multiple files

Command:

Aijian.shi@U-aijian-shi:~/ALM$ less test2.log test.log

Output:

11 ifconfig2 2 ping www.baidu.com3 3 ifconfig4 4 / / 10.128.161.108/share5 5 10.128.161.108/share6 6 ssh7 7 keygen8 8 trsa9 9 ssh10 10 .ssh / 111213 test2.log (file 1 of 2) (END)-Next: test.log

Description:

After entering: n, switch to test.log

After entering: P, switch to test2.log

Ps: when you are browsing a file, you can also use the: e command to open another file.

Command:

Less file1:e file2

5. Additional remarks

1. Full screen navigation

Ctrl + F-move forward one screen

Ctrl + B-move backwards one screen

Ctrl + D-move half screen forward

Ctrl + U-move half screen backward

two。 One-way navigation

J-move forward one line

K-move backward one line

3. Other navigation

G-move to the last line

G-move to the first line

Q / ZZ-exits the less command

4. Other useful commands

V-use the configured editor to edit the current file

H-displays help documentation for less

& pattern-displays only the lines that match the pattern, not the entire file

5. Tag navigation

When viewing a large file using less, you can mark it anywhere, and you can navigate to a text location marked with a specific tag through the command:

Ma-use a to mark the current position of the text

'a-navigate to mark a

6. Find

Both more and less have the search function. Press / then enter the string you are looking for, then press Enter, press n (next) to continue looking, uppercase N is to look back (above), press Q (quit) or ZZ to leave

This is the end of the introduction to "how to use Linux's more and less commands". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report