Linux 中的 less 命令和示例
Less 命令是一个 Linux 实用程序,可用于一次一页(一个屏幕)读取文本文件的内容。它具有更快的访问速度,因为如果文件很大,它不会访问完整的文件,而是逐页访问它。
例如,如果它是一个大文件并且您正在使用任何文本编辑器读取它,那么完整的文件将被加载到主内存中。 less 命令不会加载整个文件,而是逐个加载它,这使得它更快。
句法 :
less filename
注意:在以下示例中,我使用 dmesg 输出作为 less 命令的输入。
例如:如果你想读取 dmesg 命令的内容,最好使用 less 命令
dmesg | less
输出 :
[ 0.000000] microcode: microcode updated early to revision 0x21, date = 2017-11-20
[ 0.000000] random: get_random_bytes called from start_kernel+0x42/0x504 with crng_init=0
[ 0.000000] Linux version 4.13.0-26-generic (buildd@lgw01-amd64-031) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)) #29~16.04.
2-Ubuntu SMP Tue Jan 9 22:00:44 UTC 2018 (Ubuntu 4.13.0-26.29~16.04.2-generic 4.13.13)
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.13.0-26-generic.efi.signed root=UUID=993a37f2-7ea9-43a3-b652-5b26bb879797 ro quiet splash vt.handoff=7
[ 0.000000] KERNEL supported cpus:
[ 0.000000] Intel GenuineIntel
[ 0.000000] AMD AuthenticAMD
[ 0.000000] Centaur CentaurHauls
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers'
[ 0.000000] x86/fpu: xstate_offset[2]: 576, xstate_sizes[2]: 256
[ 0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format.
[ 0.000000] e820: BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000006efff] usable
[ 0.000000] BIOS-e820: [mem 0x000000000006f000-0x000000000006ffff] reserved
[ 0.000000] BIOS-e820: [mem 0x0000000000070000-0x0000000000087fff] usable
[ 0.000000] BIOS-e820: [mem 0x0000000000088000-0x00000000000bffff] reserved
[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x0000000094d5ffff] usable
[ 0.000000] BIOS-e820: [mem 0x0000000094d60000-0x0000000095d5ffff] reserved
[ 0.000000] BIOS-e820: [mem 0x0000000095d60000-0x000000009a36efff] usable
[ 0.000000] BIOS-e820: [mem 0x000000009a36f000-0x000000009aebefff] reserved
[ 0.000000] BIOS-e820: [mem 0x000000009aebf000-0x000000009afbefff] ACPI NVS
[ 0.000000] BIOS-e820: [mem 0x000000009afbf000-0x000000009affefff] ACPI data
[ 0.000000] BIOS-e820: [mem 0x000000009afff000-0x000000009affffff] usable
[ 0.000000] BIOS-e820: [mem 0x000000009b000000-0x000000009f9fffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000e0000000-0x00000000efffffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000fe101000-0x00000000fe112fff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000feb00000-0x00000000feb0ffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000fec00000-0x00000000fec00fff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000fed00000-0x00000000fee00fff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000ffc00000-0x00000000ffffffff] reserved
[ 0.000000] BIOS-e820: [mem 0x0000000100000000-0x000000025f5fffff] usable
[ 0.000000] NX (Execute Disable) protection: active
[ 0.000000] e820: update [mem 0x93c97018-0x93ca7057] usable ==> usable
[ 0.000000] e820: update [mem 0x93c97018-0x93ca7057] usable ==> usable
[ 0.000000] e820: update [mem 0x93c8a018-0x93c96057] usable ==> usable
[ 0.000000] e820: update [mem 0x93c8a018-0x93c96057] usable ==> usable
:
最常用的选项:
-E : causes less to automatically exit the first time it reaches end of file.
-f : forces non-regular file to open.
-F : causes less to exit if entire file can be displayed on first screen
-g : highlight the string which was found by last search command
-G : suppresses all highlighting of strings found by search commands
-i : cause searches to ignore case
-n : suppresses line numbers
-p pattern : it tells less to start at the first occurrence of pattern in the file
-s : causes consecutive blank lines to be squeezed into a single blank line
带选项的命令用法:
dmesg | less -p "failure"
上面的命令告诉 less 在文件中第一次出现模式“失败”时开始。
输出 :
[ 368.748104] wlp2s0: failed to remove key (1, ff:ff:ff:ff:ff:ff) from hardware (-22)
[ 372.254014] wlp2s0: authenticate with a0:55:4f:27:bd:01
[ 372.257112] wlp2s0: send auth to a0:55:4f:27:bd:01 (try 1/3)
[ 372.261055] wlp2s0: authenticated
[ 372.264307] wlp2s0: associate with a0:55:4f:27:bd:01 (try 1/3)
[ 372.270621] wlp2s0: RX AssocResp from a0:55:4f:27:bd:01 (capab=0x431 status=0 aid=199)
[ 372.272312] wlp2s0: associated
[ 372.357068] wlp2s0: Limiting TX power to 30 (30 - 0) dBm as advertised by a0:55:4f:27:bd:01
[ 682.255302] wlp2s0: disassociated from a0:55:4f:27:bd:01 (Reason: 1=UNSPECIFIED)
[ 682.304134] wlp2s0: failed to remove key (1, ff:ff:ff:ff:ff:ff) from hardware (-22)
[ 685.809837] wlp2s0: authenticate with a0:55:4f:27:bd:01
dmesg | less -N
它将显示输出以及行号
输出 :
1 [ 0.000000] microcode: microcode updated early to revision 0x21, date = 2017-11-20
2 [ 0.000000] random: get_random_bytes called from start_kernel+0x42/0x504 with crng_init=0
3 [ 0.000000] Linux version 4.13.0-26-generic (buildd@lgw01-amd64-031) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)) #2 3 9~16.04.2-Ubuntu SMP Tue Jan 9 22:00:44 UTC 2018 (Ubuntu 4.13.0-26.29~16.04.2-generic 4.13.13)
4 [ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.13.0-26-generic.efi.signed root=UUID=993a37f2-7ea9-43a3-b652-5b26bb879797 ro qu 4 iet splash vt.handoff=7
5 [ 0.000000] KERNEL supported cpus:
6 [ 0.000000] Intel GenuineIntel
7 [ 0.000000] AMD AuthenticAMD
8 [ 0.000000] Centaur CentaurHauls
9 [ 0.000000] x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'
10 [ 0.000000] x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers'
11 [ 0.000000] x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers'
12 [ 0.000000] x86/fpu: xstate_offset[2]: 576, xstate_sizes[2]: 256
less -F filename
eg. less -F /home/mandeep/test/first.erl
它不会给出任何输出,因为文件可以在单个屏幕中显示。
less /home/mandeep/test/first.erl
以上是测试文件的内容,可以单屏显示。
-module(first).
-export([fib/1]).
fib(X) when X
1;
fib(X) when X >= 2 ->
fib(X - 1) + fib(X - 2).
–曼迪普·辛格
参考 :
1)维基百科
2) linux 手册页