linux命令 less_如何在Linux上使用less命令

linux命令 less

linux命令 less

A Linux terminal on a Ubuntu-style desktop.
Fatmawati Achmad Zaenuri/Shutterstock Fatmawati Achmad Zaenuri / Shutterstock

The less command lets you page through a text file, displaying a screenful of text each time. It seems like one of the simplest Linux commands at first glance, but there’s a lot more to less than meets the eye.

less命令可让您分页浏览文本文件,每次都显示一小段文本。 乍看之下,这似乎是最简单Linux命令之一,但还有很多比它还让人眼花meet乱。

少的历史 (The History of less)

Everything in Linux–and Unix—has a history, no pun intended. The less program is based on the more program, which was initially released in 1978 in version 3.0 of the Berkeley Software Distribution Unix (3.0BSD). more allowed you to progressively page through a text file, displaying a screenful of text at a time.

Linux和Unix中的所有内容都有悠久的历史,并非一帆风顺。 less程序基于more程序,该程序最初于1978年在Berkeley Software Distribution Unix (3.0BSD)的3.0版中发布。 more让您逐步浏览文本文件,一次显示一屏文本。

Necessity being the mother of invention, it was the inability of early versions of more to scroll backward through a file that prompted Mark Nudelman to develop less and to overcome that specific problem. That work started in 1983, and the first version was released outside of the company he worked for in 1985. As of October 2019, he is still the maintainer of less.

需要是发明之母,这是早期版本的无能more通过促使马克·诺德尔曼制定文件滚动落后less和克服特定问题。 这项工作始于1983年,并且第一个版本在1985年供职的公司之外发布。截至2019年10月,他仍然less 的维护者

I wonder if there is a Linux user who hasn’t used less? Even if they haven’t used it to page through a chosen text file, chances are they’ve used the man command. And man calls less behind the scenes to display the man pages.

我想知道是否有没有less使用LinuxLinux用户? 即使他们没有使用它来翻页选定的文本文件,也有可能他们使用了man命令。 而且, man less在幕后调用来显示man页。

This is a command with a lot of tricks up its sleeve.

这是一条带有很多技巧的命令。

为什么少胜于多 (Why less Is Better Than more)

less has been added to steadily over the years. It has a staggering amount of command-line options and in-application command keystrokes. Do a quick comparison of the man page for less and the man page for more , and you’ll start to see how less absolutely towers over more.

多年来,稳定地增加了less的内容。 它具有数量惊人的命令行选项和应用程序内命令击键。 快速对比手册页少些和手册页 more ,您将开始看到绝对less more

more has overcome its initial failing of not being able to page backward through text, but only for files. It cannot page backward through piped input. You can do that with less.

more功能克服了最初无法通过文本向后翻页(仅适用于文件)的失败。 它无法通过管道输入向后翻页。 您可以用less做到这less

With its flexibility in navigating files, viewing multiple files, searching for text, dropping and returning to bookmarks, and dealing with piped input, less wins hands down. Use less instead of the more .

凭借其在浏览文件,查看多个文件,搜索文本,删除和返回书签和处理管道输入,灵活性less胜手了。 使用less而不是more

减少读取文件 (Reading a File With less)

To load a file into less, provide the name of the file on the command line:

要将文件加载到less ,请在命令行中提供文件名:

less Dr-Jekyll-and-Mr-Hyde-001.txt
less Dr-Jekyll-and-Mr-Hyde-001.txt in a terminal window

The file is loaded and displayed. The top (or “start”) of the file is shown in the terminal window. You can use the scroll wheel of your mouse to scroll forward and backward through the text.

文件已加载并显示。 文件的顶部(或“开始”)显示在终端窗口中。 您可以使用鼠标的滚轮在文本中前后滚动。

On the keyboard, use the Space bar or Page Down key to move forward through the text one screenful of text at a time.

在键盘上,使用空格键或Page Down键可一次向前浏览一屏。

Page Up will move backward through the file (towards the “start” of the file.) The Home and End keys will take you directly to the start and end of the text file, respectively.

Page Up将在文件中向后移动(朝文件的“开始”。)Home和End键分别将您直接带到文本文件的开头和结尾。

less displaying a text file in a terminal window

The name of the file is displayed in the bottom-left corner of the display. When you start to move around in the file, the bottom line is cleared. It is used to display messages to you, and for you to enter commands in.

文件名显示在显示屏的左下角。 当您开始在文件中移动时,将清除底行。 它用于向您显示消息,并供您输入命令。

Press “q” to quit less.

按“ q” less退出。

显示行号 (Displaying Line Numbers)

To have the lines of the text file numbered for you, use the -N (line numbers) option.

要为文本文件的行编号,请使用-N (行号)选项。

less -N Dr-Jekyll-and-Mr-Hyde-001.txt
less -N Dr-Jekyll-and-Mr-Hyde-001.txt in a terminal window

The line numbers can be useful to guide you back to specific lines or sections within log files and other files that are not written in standard prose.

行号对于引导您返回日志文件和其他非标准散文文件中的特定行或节很有用。

less displaying a text file with line numbers in a terminal window

搜索更少 (Searching in less)

To search through the text of the file, press “/” and then type your search phrase. The search is case-sensitive. Your search phrase is displayed on the bottom line of the display. Hit “Enter” to perform the search.

要搜索文件的文本,请按“ /”,然后键入搜索短语。 搜索区分大小写。 您的搜索短语显示在显示屏的底行。 点击“ Enter”执行搜索。

In this example, the search term is “Enfield,” and this can be seen at the bottom of the display.

在此示例中,搜索词为“ Enfield”,这可以在显示的底部看到。

Searching for "Enfield" in less

The search takes place from the current page to the end of the text file. To search the entire file, move to the top of the file before you search.

搜索从当前页面到文本文件的末尾。 要搜索整个文件,请在搜索之前移至文件顶部。

You will be told if there are no matches. If a match is found, the display moves to display the found item.

如果没有匹配项,则会通知您。 如果找到匹配项,则显示屏将移动以显示找到的项目。

less displaying a matching search item

To find the next matching item, press “n”. To search for the previous matching item, press “N”.

要查找下一个匹配项,请按“ n”。 要搜索上一个匹配项,请按“ N”。

less with two matching search items

To search backward from your current position in the file toward the start of the file, press the “?” key and type your search term. To find the next matching item, press “n”. To search for the previous matching item, press “N”.

要从文件中的当前位置向文件的开头向后搜索,请按“?” 键并输入您的搜索字词。 要查找下一个匹配项,请按“ n”。 要搜索上一个匹配项,请按“ N”。

Note that, when you’re searching backward, the next matching item (found with “n”) is the next one nearer to the top of the file, and the “N” for the previous matching item looks for a matching item closer to the bottom of the file. in other words, “n” and “N” reverse their search direction when you search backward.

请注意,当您向后搜索时,下一个匹配项(找到“ n”)是靠近文件顶部的下一个,而上一个匹配项的“ N”则寻找更接近文件顶部的匹配项文件的底部 。 换句话说,当您向后搜索时,“ n”和“ N”会反转其搜索方向。

打开带有搜索词的文件 (Opening a File With a Search Term)

You can use the -p (pattern) option to cause less to search through the text file and find the first matching item. It will then display the page with the matching search item in it, instead of the first page of the file. Unless, of course, the search item is found on the first page of the file.

您可以使用-p (模式)选项来less搜索文本文件并找到第一个匹配项的可能性。 然后,它将显示其中包含匹配搜索项的页面,而不是文件的第一页。 当然,除非在文件的第一页上找到搜索项。

Note there is no space between the -p and the search term.

注意, -p和搜索词之间没有空格。

less -pEnfield Dr-Jekyll-and-Mr-Hyde-001.txt
less -pEnfield Dr-Jekyll-and-Mr-Hyde-001.txt

The file is displayed with the first matching search term highlighted.

将显示文件,并突出显示第一个匹配的搜索词。

less displaying a file with the first matching search item highlighted

更少浏览:最有用的键 (Navigating in Less: The Most Useful Keys)

Use these keys to move and search through the text file.

使用这些键在文本文件中移动和搜索。

  • Move forward one line: Down Arrow, Enter, e, or j

    向前移动一行 :向下箭头,Enter,e或j

  • Move backward one line: Up Arrow, y, or k

    向后移动一行 :向上箭头,y或k

  • Move forward one page: Space bar or Page Down

    向前移动一页 :空格键或下一页

  • Move backward one page: Page Up or b

    向后移动一页 :Page Up或b

  • Scroll to the right: Right Arrow

    向右 滚动 :向右箭头

  • Scroll to the left: Left Arrow

    向左 滚动 :向左箭头

  • Jump to the top of the file: Home or g

    跳到文件顶部 :Home或g

  • Jump to the end of the file: End or G

    跳转到该文件的结束 :结束或G

  • Jump to a specific line: Type the line number  and then hit “g”

    跳到特定 :输入行号,然后按“ g”

  • Jump to a percentage way through the file: Type the percentage and then hit “p” or “%.” (You can even enter decimal values, so to jump to the point 27.2 percent through the file, type “27.2” and then hit “p” or “%.”  Why would you want to use decimals? I honestly have no idea.)

    跳转到文件的百分比方式:键入百分比,然后按“ p”或“%”。 (您甚至可以输入十进制值,因此要在文件中跳至27.2%,键入“ 27.2”,然后单击“ p”或“%。”为什么要使用十进制?老实说,我不知道。)

  • Search forward: Hit “/” and type your search, like “/Jekyll”, and press Enter

    向前搜索 :按“ /”并输入搜索内容,例如“ / Jekyll”,然后按Enter

  • Search backward: Hit “?” and type your search, like “/Hyde”, and press Enter

    向后搜索 :打“?” 并输入搜索内容(例如“ / Hyde”),然后按Enter

  • Next matching search item: n

    下一个匹配的搜索项目 :n

  • Previous matching search item: N

    上一个匹配的搜索项目 :N

  • Quit: q

    退出 :q

挤压空白行 (Squeeze Blank Lines)

The -s (squeeze blank lines) option removes a series of blanks lines and replaces them with a single blank line.

-s (压缩空白行)选项将删除一系列空白行,并将其替换为单个空白行。

There are a couple of consecutive blank lines in our example file, let’s see how less treats them when we use the -s option:

有一对夫妇在我们的示例文件连续的空行,让我们看看如何less对待他们,当我们使用-s选项:

less -s Dr-Jekyll-and-Mr-Hyde-001.txt
less -s Dr-Jekyll-and-Mr-Hyde-001.txt in a terminal window

All of the double (or more) blank lines have been replaced by a single blank line in each case.

在每种情况下,所有的双(或更多)空白行均已替换为单个空白行。

Less with no sequences of multiple blank lines being displayed

查看多个文件 (Viewing Multiple Files)

less can open multiple files for you. You can hop back and forth from file to file. less will remember your position in each file.

less可以为您打开多个文件。 您可以在文件之间来回跳动。 less会记住您在每个文件中的位置。

less Dr-Jekyll-and-Mr-Hyde-001.txt Dr-Jekyll-and-Mr-Hyde-002.txt
less Dr-Jekyll-and-Mr-Hyde-001.txt Dr-Jekyll-and-Mr-Hyde-002.txt in a terminal window

The files are opened, and the first file is displayed. You are shown which file you are looking at, and how many files have been loaded. This is highlighted below.

将打开文件,并显示第一个文件。 系统将显示您正在查看的文件以及已加载的文件数。 这在下面突出显示。

less with two files loaded

To view the next file, press “:” and then hit “n”.

要查看下一个文件,请按“:”,然后按“ n”。

Your display will change to show the second file, and the information on the bottom line is updated to show you are viewing the second file. This is highlighted below.

您的显示将更改为显示第二个文件,并且最下面一行的信息已更新为显示您正在查看第二个文件。 这在下面突出显示。

viewing the second file in less

To move to the previous file, type “:” and then hit “p.”

要移至上一个文件,请键入“:”,然后单击“ p”。

使用标记 (Using Marks)

less lets you drop a marker so that you can easily return to a marked passage. Each marker is represented by a letter. To drop a mark on the top-most displayed line, press “m” and then hit the letter you wish to use, such as “a”.

less使您可以放下标记,以便轻松返回标记的段落。 每个标记由一个字母表示。 要在最上方显示的行上放置标记,请按“ m”,然后单击您要使用的字母,例如“ a”。

When you press “m”, the bottom line of the display shows a prompt as it waits for you to press a letter key.

当您按“ m”时,显示屏的底行会显示一条提示,提示您等待按字母键。

less prompting for a mark

As soon as you press a letter, the prompt is removed.

按下字母后,提示即被删除。

From any other location within the file, you can easily return to a mark by pressing the apostrophe (or single quote) “‘” and then pressing the letter of the mark you wish to return to. When you press the “‘” key, you are prompted for the mark you wish to go to.

在文件中的任何其他位置,您都可以通过按撇号(或单引号)“'”然后按想要返回的标记字母来轻松返回标记。 当您按“'”键时,系统会提示您输入要转到的标记。

less prompting for a mark to return to

Press the letter of the mark you wish to return to, and that section of the text file is displayed for you.

按下要返回的标记字母,文本文件的该部分就会显示出来。

less returning to a mark

使用更少的管道输入 (Using Piped Input with Less)

less can display information that comes as a stream of piped text, just as easily as if it were a file.

less可以显示作为管道文本流的信息,就像文件一样容易。

The dmesg command displays the kernel ring buffer messages. We can pipe the output from dmesg  into less using the following command:

dmesg命令显示内核环形缓冲区消息 。 我们可以使用以下命令将dmesg的输出dmesgless

dmesg | less
dmesg | less in a terminal window

The output from dmesg is displayed.

显示dmesg的输出。

The output from dmesg in less

You can page and search through the piped input just as though it were a file. To see the most recent messages, hit “End” to go to the bottom of the file.

您可以分页和搜索管道输入,就像文件一样。 要查看最新消息,请单击“结束”以转到文件底部。

Tme most recent dmesg messages at the bottom of the file in less

As new messages arrive, you must keep pressing “End” to force less to display the bottom of the file. This isn’t very convenient. To have less always show the bottom of the text, even when new data is being added, use the +F (forward) option. Note the use of + and not - as the option flag.

随着新消息的到来,您必须继续按“结束”以less显示该文件底部的压力。 这不是很方便。 有less始终显示文本的底部,甚至当添加新的数据,使用+F (前锋)选项。 请注意,使用+而不是-作为选项标志。

dmesg | less +F
dmesg | less +F in a terminal window

The + option flag tells less to treat the option as though you had used that command inside less. So if you forgot to use the +F option, press “F” inside less .

+选项标志告诉less的对待选项,就像您 less 里面使用了该命令一样 。 所以,如果你忘记用+F选项,按“F”里面less

less awaiting new input from dmesg

less displays the bottom of the text, which shows the most recent messages from dmesg. It displays a message that it is waiting for more data. When more kernel messages appear, the display scrolls so that you can always see the newest messages.

less显示文本的底部,该文本显示dmesg最新消息。 它显示一条消息,表示它正在等待更多数据。 当出现更多内核消息时,显示滚动,以便您始终可以看到最新消息。

You can’t scroll or page in this mode; it is devoted to displaying the bottom of the piped text. To exit from of his mode, press Ctrl+c, and you will be returned to the usual less interactive mode.

您无法在此模式下滚动或翻页; 它专用于显示管道文本的底部。 要退出他的模式,请按Ctrl + c,您将返回到通常less交互的模式。

少编辑文件 (Editing Files With less)

You can edit files with less—well, sort of. This command can’t edit files, but if you type “v” when you are viewing a file, the file is transferred to your default editor. When you leave the editor, you are returned to less.

您可以用less来编辑文件。 该命令无法编辑文件,但是如果您在查看文件时键入“ v”,则文件将被传输到默认编辑器。 离开编辑器后,您将返回到less

Hit “v” when viewing a file in less:

less查看文件时,按“ v”:

file displayed in less

The file is loaded into the default editor, in this case nano:

该文件被加载到默认编辑器中,在本例中为nano

file loaded in the nano editor

When you close the editor, you are turned to less.

关闭编辑器后,您将转到less

综上所述 (In Summary)

As counterintuitive as it may seem, in this caseless > more.

看起来似乎违反直觉,在这种情况下, less > more

翻译自: https://www.howtogeek.com/444233/how-to-use-the-less-command-on-linux/

linux命令 less

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值