less command in Linux with Examples

31 Jul, 2021
Less command is a Linux utility that can be used to read the contents of a text file one page(one screen) at a time. It has faster access because if file is large it doesn’t access the complete file, but accesses it page by page. 
For example, if it’s a large file and you are reading it using any text editor, then the complete file will be loaded to main memory. The less command doesn’t load the entire file, but loads it part by part which makes it faster. 
Syntax : 
less filename
Note : I’m using dmesg output as input to less command in following examples. 
For Example : If you want to read contents of dmesg command, it’s better to use it with less command  
dmesg | less
Output :  
[    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
:
mostly used Options :  
-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  
Command Usage with options :  
dmesg | less -p "failure"
The above command tells less to start at first occurrence of pattern “failure” in the file. 
Output :  
[  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
It will show output along with line numbers Output :  
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
It will not give any output, since file can be displayed in single screen.  
less /home/mandeep/test/first.erl
These are the contents of above tested file, it can be displayed on single screen.  
-module(first).
-export([fib/1]).

fib(X) when X 
    1;
fib(X) when X >= 2 ->
    fib(X - 1) + fib(X - 2).
– Mandeep Singh 
References : 1) wikipedia 2) linux man pages 
 
molluskcreature
linux-command
Linux-text-processing-commands
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
tar command in Linux with examples
curl command in Linux with Examples
Conditional Statements | Shell Script
Tail command in Linux with examples
Docker - COPY Instruction
UDP Server-Client implementation in C
scp command in Linux with Examples
echo command in Linux with Examples
Cat command in Linux with examples
touch command in Linux with Examples