4 Practical “less” Command Examples and tips for effective navigation in Linux:

«
»

This tutorial explains Linux “less” command, options and its usage with examples.

“less” command is used to view files instead of opening the file. This post describes “less” command used in Linux along with usage.

Usage:
less [options] file_name

advertisement

* Less is a program similar to more but which allows backward movement in the file as well as forward movement.
* less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi.
* Less uses termcap (or terminfo on some systems), so it can run on a variety of terminals.

The navigation keys in less command are similar to Vim editor. In this article, We will look at few less command navigation and other operations
related to it.

1. To open the file(less file_name):

sanfoundry-> less sample.txt
 
Line 1
Hi i am a good boy.
I am x . 
Be cool and calm 
Happy birthday 
I am  Bond 00 7 
Eurekaa !!! I Got it !
Yes 
No 
NOoooooooo
Yesssssssssssssss yes 
sample.txt (END)

In order to quit from the prompt press “q” from the ketboard.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

2. To Clear screen before displaying(less -c file_path):

sanfoundry-> less sample.txt
 
Line 1
Hi i am a good boy.
I am x . 
Be cool and calm 
Happy birthday 
I am  Bond 00 7 
Eurekaa !!! I Got it !
Yes 
No 
NOoooooooo
Yesssssssssssssss yes
~
~
~
~
..
 
sample.txt (END)

It clears the screen and shown only the contents of the file.

advertisement

3. To Starts up the file from the given number(less +number file_path):

sanfoundry-> less +4 sample.txt 
 
Be cool and calm 
Happy birthday 
I am  Bond 00 7 
Eurekaa !!! I Got it !
Yes 
No 
NOoooooooo
Yesssssssssssssss yes
~
~
~
~
..
 
sample.txt (END)

4. To see large outputs one-screen-full at a time(command | less):

sanfoundry-> ls -la | less
 
drwxr-xr-x  2 root     root       4096 Apr  1  2012 .wireshark
drwxrwxr-x  4 himanshu himanshu   4096 Jun 30 01:33 workspace
-rw-------  1 himanshu himanshu    137 Jul  1 18:20 .Xauthority
-rw-------  1 himanshu himanshu   3286 Jul  1 18:34 .xsession-errors
-rw-------  1 himanshu himanshu   5556 Jul  1 16:46 .xsession-errors.old

Now let us see some Tips for effective navigation in less command

advertisement

keyboard shortcuts for “less”

 *   [Arrows]/[Page Up]/[Page Down]/[Home]/[End]: Navigation.
 *   [Space bar]: Next page.
 *   b: Previous page.
 *   ng: Jump to line number n. Default is the start of the file.
 *   nG: Jump to line number n. Default is the end of the file.
 *   /pattern: Search for pattern. Regular expressions can be used.
 *   n: Go to next match (after a successful search).
 *   N: Go to previous match.
 *   ‘^ or g: Go to start of file.
 *   ‘$ or G: Go to end of file.
 *   s: Save current content (got from another program like grep) in a file.
 *   =: File information.
 *   F: continually read information from file and follow its end. Useful for logs watching. Use Ctrl+C to exit this mode.
 *   -option: Toggle command-line option -option.
 *   h: Help.
 *   q or zz: Quit.

Forward Search Navigation

  *  / – search for a pattern which will take you to the next occurrence.
  *  n – for next match in forward
  *  N – for previous match in backward

Backward Search Navigation

advertisement
  *  ? – search for a pattern which will take you to the previous occurrence.
  *  n – for next match in forward
  *  N – for previous match in backward

Line Navigation

  *  j – navigate forward by one line
  *  k – navigate backward by one line

Screen Navigation

  *  CTRL+D – forward half window
  *  CTRL+U – backward half window
  *  CTRL+F – forward one window
  *  CTRL+B – backward one window

Count numbers as options

  *  5j – 5 lines forward.
  *  10k – 10 lines backward.

Some more less options

  *  :p - Examine the previous file in the command line list.
  *  :d - Remove the current file from the list of files.
  *  v – using the configured editor edit the current file.
  *  &pattern – display only the matching lines, not all.
  *  CTRL+G – show the current file name along with line, byte and percentage statistics.
  *  mx – mark the current position with the letter ‘x’.
  *  ‘x – go to the marked position ‘x’.

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.