Article directory
Linux Less Commands
less
is a tool for viewing file contents in a Linux environment. It allows users to scroll through files interactively. Compared with other file reading commands such as more
or provide more functionality and flexibility. cat
, less
1. Introduction to Less command
less
The command is derived from the more
command, but adds functions such as reverse navigation and search. Most importantly, unlike more
and cat
, less
does not have to read the entire file at once. This is useful for large files because users can start browsing the file immediately without waiting for the file to fully load.
2. Basic usage
less filename
Using less
is very simple, just enter the less
command, followed by the file name you want to view:
less filename
In less
environments, you can use the arrow keys or the Page Up/Page Down keys to scroll through files. Press the q
key to exit less
.
<command> | less
You can also execute the command to obtain the output result, and then pass the result to the less command for processing through a pipeline:
ffprobe -show_packets groundhog.mp4 | less
3. Common command line options
less
The command supports many command line options to change its behavior. Here are some commonly used options:
-N
: Display line number-m
: Display more detailed prompt information-E
: Automatically exit after the file ends-S
: disable automatic word wrapping
For example, to view a file with line numbers, you can use the following command:
less -N filename
4. Advanced techniques and usage
4.1 Search content
Inless
, you can search forward through the content using the /
characters followed by the search pattern, or ?
Followed by search mode to search content backwards. For example, to search for "example" in a file, enter /example
and press Enter.
4.2 Marks and jumps
less
enables the use of tags to quickly navigate to a specific location in a file. Use the command m
followed by a letter to set a tag, use '
(single quote) followed by a letter to jump to a tag.
4.3 View multiple files
less
Multiple files can be opened at the same time. Just list all the file names on the command line:
less file1 file2 file3
Inless
, you can use the :n
command to jump to the next file, and the :p
command to jump to Previous file.
5. less command usage documentation
下面是LESS命令的摘要:
标有 * 的命令可能前面会有一个数字N。
括号内的注释表示如果给出N,行为将如何改变。
前面有插入符号的键表示Ctrl键;因此^K表示ctrl-K。
h H 显示这个帮助。
q :q Q :Q ZZ 退出。
---------------------------------------------------------------------------
移动
e ^E j ^N CR * 向前移动一行(或N行)。
y ^Y k ^K ^P * 向后移动一行(或N行)。
f ^F ^V SPACE * 向前移动一个窗口(或N行)。
b ^B ESC-v * 向后移动一个窗口(或N行)。
z * 向前移动一个窗口(并将窗口设定为N)。
w * 向后移动一个窗口(并将窗口设定为N)。
ESC-SPACE * 向前移动一个窗口,但不停在文件结束处。
d ^D * 向前移动半个窗口(并将半窗口设定为N)。
u ^U * 向后移动半个窗口(并将半窗口设定为N)。
ESC-) RightArrow * 右移半个屏幕宽度(或N个位置)。
ESC-( LeftArrow * 左移半个屏幕宽度(或N个位置)。
ESC-} ^RightArrow 移到最后一列显示。
ESC-{
^LeftArrow 移到第一列。
F 向前永远;类似于 "tail -f"。
ESC-F 类似于F,但在找到搜索模式时停止。
r ^R ^L 重绘屏幕。
R 重绘屏幕,丢弃缓冲输入。
---------------------------------------------------
默认的“窗口”是屏幕高度。
默认的“半窗口”是屏幕高度的一半。
---------------------------------------------------------------------------
搜索
/pattern * 向前搜索(N-th)匹配行。
?pattern * 向后搜索(N-th)匹配行。
n * 重复上次搜索(N-th出现)。
N * 反向重复上次搜索。
ESC-n * 重复上次搜索,跨文件。
ESC-N * 反向重复上次搜索,跨文件。
ESC-u 撤销(切换)搜索高亮。
&pattern * 仅显示匹配行
---------------------------------------------------
搜索模式可能以一个或多个以下符号开始:
^N 或 ! 搜索不匹配的行。
^E 或 * 搜索多个文件(通过文件结束)。
^F 或 @ 在第一个文件(对于/)或最后一个文件(对于?)开始搜索。
^K 高亮匹配,但不移动(保持位置)。
^R 不使用正则表达式。
---------------------------------------------------------------------------
跳转
g < ESC-< * 跳转到文件的第一行(或第N行)。
G > ESC-> * 跳转到文件的最后一行(或第N行)。
p % * 跳转到文件的开头(或文件的N%位置)。
t * 跳转到下一个(第N个)标签。
T * 跳转到上一个(第N个)标签。
{
( [ * 查找关闭的括号 } ) ]。
} ) ] * 查找打开的括号 {
( [。
ESC-^F <c1> <c2> * 查找关闭的括号 <c2>。
ESC-^B <c1> <c2> * 查找打开的括号 <c1>
---------------------------------------------------
每一个"查找关闭括号"命令都会向前找到与顶行中的(第N个)打开括号匹配的关闭括号。
每一个"查找打开括号"命令都会向后找到与底行中的(第N个)关闭括号匹配的打开括号。
m<字母> 使用<字母>标记当前的顶行。
M<字母> 使用<字母>标记当前的底行。
'<字母> 跳转到之前标记的位置。
'' 跳转到上一个位置。
^X^X 同 '。
ESC-M<字母> 清除一个标记。
---------------------------------------------------
标记可以是任何大写或小写的字母。
有些标记是预定义的:
^ 代表 文件的开头
$ 代表 文件的结尾
---------------------------------------------------------------------------
切换文件
:e [file] 检查一个新文件。
^X^V 同 :e。
:n * 检查命令行中的下一个(第N个)文件。
:p * 检查命令行中的上一个(第N个)文件。
:x * 检查命令行中的第一个(或第N个)文件。
:d 从命令行列表中删除当前文件。
= ^G :f 打印当前文件名。
---------------------------------------------------------------------------
其他命令
-<标志> 切换命令行选项[见下面的OPTIONS]。
--<名称> 通过名称切换命令行选项。
_<标志> 显示命令行选项的设置。
__<名称> 通过名称显示选项的设置。
+cmd 每次检查新文件时执行less cmd。
!command 使用$SHELL执行shell命令。
|Xcommand 将当前位置和标记X之间的文件通过管道传输给shell命令。
s file 将输入保存到一个文件。
v 使用$VISUAL或$EDITOR编辑当前文件。
V 打印"less"的版本号。
---------------------------------------------------------------------------
选项
大多数选项可以在命令行上更改,
或者在less中使用-或--命令来更改。
选项可以有两种形式:一个是前面带有-的单个字符,
另一个是前面带有--的名称。
-? ........ --help
显示帮助(来自命令行)。
-a ........ --search-skip-screen
搜索跳过当前屏幕。
-A ........ --SEARCH-SKIP-SCREEN
搜索开始于目标行之后。
-b [N] .... --buffers=[N]
缓冲区数量。
-B ........ --auto-buffers
不为管道自动分配缓冲区。
-c ........ --clear-screen
通过清除而不是滚动来重绘。
-d ........ --dumb
哑终端。
-D [xn.n] . --color=xn.n
设置屏幕颜色。(仅限MS-DOS)
-e -E .... --quit-at-eof --QUIT-AT-EOF
在文件结束时退出。
-f ........ --force
强制打开非常规文件。
-F ........ --quit-if-one-screen
如果整个文件适合在第一屏显示,则退出。
-g ........ --hilite-search
仅高亮显示搜索的最后一个匹配。
-G ........ --HILITE-SEARCH
不高亮显示任何搜索匹配。
-h [N] .... --max-back-scroll=[N]
向后滚动限制。
-i ........ --ignore-case
在搜索中忽略大小写,前提是搜索不包含大写字母。
-I ........ --IGNORE-CASE
在所有搜索中忽略大小写。
-j [N] .... --jump-target=[N]
目标行的屏幕位置。
-J ........ --status-column
在屏幕左边缘显示状态列。
-k [file] . --lesskey-file=[file]
使用lesskey文件。
-K ........ --quit-on-intr
响应ctrl-C退出less。
-L ........ --no-lessopen
忽略LESSOPEN环境变量。
-m -M .... --long-prompt --LONG-PROMPT
设置提示样式。
-n -N .... --line-numbers --LINE-NUMBERS
不使用行号。
-o [file] . --log-file=[file]
复制到日志文件(仅限标准输入)。
-O [file] . --LOG-FILE=[file]
复制到日志文件(无条件覆盖)。
-p [pattern] --pattern=[pattern]
从命令行开始于模式。
-P [prompt] --prompt=[prompt]
定义新提示。
-q -Q .... --quiet --QUIET --silent --SILENT
静音终端铃声。
-r -R .... --raw-control-chars --RAW-CONTROL-CHARS
输出“原始”控制字符。
-s ........ --squeeze-blank-lines
压缩多个空白行。
-S ........ --chop-long-lines
切除(截断)长行而不是包裹。
-t [tag] .. --tag=[tag]
查找标签。
-T [tagsfile] --tag-file=[tagsfile]
使用备用的标签文件。
-u -U .... --underline-special --UNDERLINE-SPECIAL
更改处理退格符的方式。
-V ........ --version
显示“less”的版本号。
-w ........ --hilite-unread
在前向屏幕后高亮显示第一条新行。
-W ........ --HILITE-UNREAD
在任何前向移动后高亮显示第一条新行。
-x [N[,...]] --tabs=[N[,...]]
设置制表符停止位。
-X ........ --no-init
不使用termcap初始化/反初始化字符串。
-y [N] .... --max-forw-scroll=[N]
前向滚动限制。
-z [N] .... --window=[N]
设置窗口大小。
-" [c[c]] . --quotes=[c[c]]
设置shell引号字符。
-~ ........ --tilde
不显示文件结束后的波浪号。
-# [N] .... --shift=[N]
水平滚动量(0 = 半个屏幕宽度)
--follow-name
如果输入文件被重命名,F命令更改文件。
--mouse
启用鼠标输入。
--no-keypad
不发送termcap键盘初始化/反初始化字符串。
--no-histdups
从命令历史中删除重复项。
--rscroll=C
设置用于标记截断行的字符。
--save-marks
在less的调用中保留标记。
--use-backslash
后续选项使用反斜杠作为转义字符。
--wheel-lines=N
鼠标滚轮每点击一次移动N行。
---------------------------------------------------------------------------
行编辑
这些键可以用来编辑在屏幕底部"命令行"上输入的文本。
右箭头 ..................... ESC-l ... 光标向右移动一个字符。
左箭头 ...................... ESC-h ... 光标向左移动一个字符。
ctrl-右箭头 ESC-右箭头 ESC-w ... 光标向右移动一个单词。
ctrl-左箭头 ESC-左箭头 ESC-b ... 光标向左移动一个单词。
HOME ........................... ESC-0 ... 光标移动到行首。
END ............................ ESC-$ ... 光标移动到行尾。
BACKSPACE ................................ 删除光标左边的字符。
DELETE ......................... ESC-x ... 删除光标下的字符。
ctrl-BACKSPACE ESC-BACKSPACE ........... 删除光标左边的单词。
ctrl-DELETE .... ESC-DELETE .... ESC-X ... 删除光标下的单词。
ctrl-U ......... ESC (仅MS-DOS) ....... 删除整行。
上箭头 ........................ ESC-k ... 检索上一条命令行。
下箭头 ...................... ESC-j ... 检索下一条命令行。
TAB ...................................... 完成文件名并循环。
SHIFT-TAB ...................... ESC-TAB 完成文件名并反向循环。
ctrl-L ................................... 完成文件名,列出全部。
6. Summary
less
is a powerful file reading tool that provides rich functions, including searching, navigating and viewing multiple files. By mastering less
, you can browse and analyze files in a Linux environment more efficiently.
ᅟᅠ ᅟᅠ.. ᅟᅠ ᅟᅠ ᅟᅠ ᅟ ᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ .ᅟᅠ ᅟᅠ ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟ ᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ..ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ . ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ . ᅟᅠ ᅟᅠ ᅟᅠ ᅟ ᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ... ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟ ᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ..ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ . ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ . ᅟᅠ ᅟᅠ ᅟᅠ ᅟ ᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ... ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟ ᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ..ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ . ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ . ᅟᅠ ᅟᅠ ᅟᅠ ᅟ ᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ... ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟ ᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ..ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ . ᅟᅠ ..
ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ᅟ ᅠ .. ᅟᅠ ᅟᅠ . ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ .. ᅟᅠ ᅟᅠ ᅟᅠ ᅟᅠ ..ᅟᅠ ᅟᅠ ᅟᅠ