A Linux command a day (13): less command

post thumb
by Admin/ on 25 Sep 2021

A Linux command a day (13): less command

The less tool is also a tool for paging files or other output, and is supposed to be the orthodox linux tool for viewing the contents of files, which is extremely powerful. The usage of less is more flexible than more. With more, we can’t go to the front, only to the back, but with less, we can use [pageup], [pagedown]and other keys to go backwards and forwards, making it easier to see the contents of a file! In addition, you can have more search functions inless`, not only downward, but also upward.

1. Command format.

less [parameter] file ### 2.

2. Command function.

less is similar to more, but with less you can browse files at will, while more can only move forward, but not backward, and less does not load the whole file before viewing.

3. Command parameters.

-b <buffer size> Set the buffer size  
-e Automatically leave when the file is finished being displayed

-f force open special files, such as peripheral designators, directories and binaries  
-g flag only the last searched keyword

-i Ignore case when searching  
-m Show percentages similar to the more command

-N show line numbers per line  
-o <filename> saves the contents of less output in a specified file

-Q does not use warning tones  
-s Show consecutive blank lines

-S Trash excess lines if they are too long  
-x <number> displays the "tab" key as a specified number of spaces

/String: Search down the "string" function  
String String: function to search "string" upwards

n: repeat the previous search (with / or ?) related)  
N: repeat the previous search in reverse (with / or ?) related)

b: turn back one page  
d flip back half a page

h Display the help screen  
Q Quit the less command

u Scroll forward half a page  
y Scroll forward one line

Spacebar Scroll one line  
Enter: Scrolls one page

[pagedown]: scroll down one page  
[pageup]: scroll up one page

4. Examples of use.

Example 1: Viewing a file


less log2013.log


post thumb

Example 2: ps view process information and paginate it by less


ps -ef |less


post thumb

Example 3:View the command history and display it in less pages


history | less


[root@localhost test]# history | less  
  22 scp -r tomcat6.0.32 root@  
  23 cd .  
  24 scp -r web root@  
  25 cd soft  
  26 ls  
  27 scp -r jdk1.6.0_16/ root@

  28 clear  
  29 vim /etc/profile  
  30 vim /etc/profile  
  31 cd tomcat6.0.32/bin/  
  32 ls  
  33 . /shutdown.sh  
  34 . /startup.sh  
  35 vim startup.sh  
  36 ls  
  37 echo $JAVA_HOME  
  38 java  
  39 ls  
  40 ls  
  41 clear  
  42 cd /opt  
  43 ls  
  44 cp apache-tomcat-6.0.32.tar.gz soft/  
  45 ls  
  46 rm -f apache-tomcat-6.0.32.tar.gz   
  47 ls  
  48 cd soft  
  49 ls  
  50 tar -vzf apache-tomcat-6.0.32.tar.gz   
  51 tar -vzfx apache-tomcat-6.0.32.tar.gz   
  52 tar -zxvf apache-tomcat-6.0.32.tar.gz   
  53 ls  
  54 cd apache-tomcat-6.0.32  
  55 ls  
  56 cd .  
  57 mv apache-tomcat-6.0.32 tomcat6.0.32  
  58 ls  
  59 cd tomcat6.0.32/  
  60 ls

Example 4: Browsing Multiple Files


Less log2013.log log2014.log


post thumb


After entering: n, switch to log2014.log

After entering : p, switch to log2013.log

5. Additional Remarks

1. Full screen navigation

ctrl + F - move forward one screen

ctrl + B - move back one screen

ctrl + D - move half screen forward

ctrl + U - move back half a screen

2. Single line navigation

j - move one line forward

k - move back one line

3. Other navigation

G - move to the last line

g - move to the first line

q / ZZ - exit less command

4. Other useful commands

v - Edit the current file with the configured editor

h - displays the help file for less

&pattern - show only the lines that match the pattern, not the whole file

5. Tag navigation

When viewing large files with less, you can mark any position, and you can navigate to the location of the text marked with a specific marker by using the command

ma - mark the current position of the text with a

‘a - navigate to marker a



comments powered by Disqus