less command - display text files in pages

The less command is similar to the more command, and it also displays the file content in pages.

The less command can not only browse the file content from front to back (press the PageDown key to turn the page down), but also from the back to the front (press the PageUp key to turn the page up), which is more flexible than the more command, which can only turn the page from the front to the back Browse the file contents.

The syntax of the less command is as follows:

less [选项] 文件

Common options are as follows:

options role or meaning
-b Set the size of the buffer 
-e Automatically exit when the file display ends 
-f Force open file -g only flags the last searched keyword 
-c Do not scroll the screen, display the file content after clearing the screen
-i   Ignore case when searching
-m Show reading progress percentage 
-N display the line number of each line
-o Save the output content in the specified file 
-p Display the contents of the file starting from the line containing the specified text pattern
-Q   Do not use warning tones
-s Display multiple consecutive blank lines as one blank line 
-S When the content of a single line is long, it will be truncated and not displayed in a new line
-x Display TAB characters as specified number of space characters
-z No Num is an integer, set the window size, turn Num rows up or down each time
+cmd Execute the cmd command, such as +G means to jump to the end of the text, +18 means to jump to the 18th line, +/str1 means to jump to the line containing str1

Example demonstration:

1. View the content of the file by page

[root@myEuler ~]# less /etc/ssh/sshd_config 
……此处省略输出……

After opening the file with the less command, you can use the following common interactive keys:

  • ENTER: scroll forward one line
  • y: scroll down one line, same as down arrow key
  • d: scroll up half a screen
  • u: scroll down half a screen
  • f: Scroll up one screen, same as PageUp key
  • b: scroll down one screen, same as PageDown key, or space bar
  • g: jump to the first line of the file
  • G: jump to the end of the file
  • /PATTERN: search down for the specified text pattern PATTERN
  • n: jump to the next match
  • N: jump to the previous match
  • h: display help information
  • q: exit, or enter ZZ

2. View the content of the file by page, and display the line number at the same time

[root@myEuler ~]# less -N /etc/ssh/sshd_config
      1 #       $OpenBSD: sshd_config,v 1.104 2021/07/02 05:11:21 dtucker Exp $
      2 
      3 # This is the sshd server system-wide configuration file.  See

3. To view the contents of the file in pages, it is required to start displaying from the specified line

# 指定从第18行开始显示,且显示行号
[root@myEuler ~]# less -N +18 /etc/ssh/sshd_config 
     18 # SELinux about this change.
     19 # semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
……此处省略部分输出……

4. Cooperate with the pipeline to display the output of the command in pages

[root@myEuler ~]# ps -ef | less -N
……此处省略输出……

5. Display the file content from the line where the matching text pattern is located

# 从包含文本模式PermitRootLogin的行开始显示,并显示行号
[root@myEuler ~]# less -N -p PermitRootLogin /etc/ssh/sshd_config

6. When matching text, it is not case-sensitive, and the reading progress is displayed

# 显示行号和阅读进度比例,匹配文本host时不区分大小写
[root@myEuler ~]# less -Nmip host /etc/ssh/sshd_config 

Guess you like

Origin blog.csdn.net/u013007181/article/details/129482330