In this article, we will learn Linux less command with examples.
What is purpose of Linux less command.
- Less is a command line utility that displays the contents of a file or a command output, one page at a time.
- It is similar to more , but has more advanced features and allows you to navigate both forward and backward through the file.
- The less command is mostly used for opening large files.
- The content displayed by less can be navigated by entering keyboard shortcuts.
Linux less command Syntax:
less [options] file_name
Linux less command examples:
1. View the file with numbers using less command.
less -N /etc/passwd
2. Following command simply displays first page of a file.
less less.txt
3. By default, when less exits, the file contents will be cleared from the screen. To leave file contents on screen, use the -X option:
less -X filename
4. To Starts up the file from the given number(less +number file_path):
Following command displays file content with number and starts from line number 8.
less +8 -N /etc/passwd
5. Search file using less command.
less file_name then press / type the string to be searched. Then, press enter.
To find the next matching item, press ānā. To search for the previous matching item, press āNā.
6. We can also redirect the output from a command to less using a pipe. For example, to view the output of the ps command page by page you would type:
ps aux | less
Linux less command options(few):
For MOVING
e ^E j ^N CR * Forward one line (or N lines). y ^Y k ^K ^P * Backward one line (or N lines). f ^F ^V SPACE * Forward one window (or N lines). b ^B ESC-v * Backward one window (or N lines). z * Forward one window (and set window to N). w * Backward one window (and set window to N). ESC-SPACE * Forward one window, but don't stop at end-of-file. d ^D * Forward one half-window (and set half-window to N). u ^U * Backward one half-window (and set half-window to N). ESC-) RightArrow * Right one half screen width (or N positions). ESC-( LeftArrow * Left one half screen width (or N positions). ESC-} ^RightArrow Right to last column displayed. ESC-{ ^LeftArrow Left to first column. F Forward forever; like "tail -f". ESC-F Like F but stop when search pattern is found. r ^R ^L Repaint screen. R Repaint screen, discarding buffered input.
space bar Move down one page.
For Searching:
/pattern * Search forward for (N-th) matching line. ?pattern * Search backward for (N-th) matching line. n * Repeat previous search (for N-th occurrence). N * Repeat previous search in reverse direction. ESC-n * Repeat previous search, spanning files. ESC-N * Repeat previous search, reverse dir. & spanning files. ESC-u Undo (toggle) search highlighting. &pattern * Display only matching lines
For JUMPING JUMPING
g < ESC-< * Go to first line in file (or line N). G > ESC-> * Go to last line in file (or line N). p % * Go to beginning of file (or N percent into file). t * Go to the (N-th) next tag. T * Go to the (N-th) previous tag. { ( [ * Find close bracket } ) ]. } ) ] * Find open bracket { ( [.