Unix Less Command: 10 Tips for Effective Navigation
Less Command – Search Navigation
Once you’ve opened a log file (or any file) using less file-name, use the following keys to search. Please note that the match will be highlighted automatically by default.
- / – search for a pattern which will take you to the next occurrence.
- n – for next match in forward
- N – for previous match in backward
- ? – search for a pattern which will take you to the previous occurrence.
- n – for next match in backward direction
- N – for previous match in forward direction
Less Command – Screen Navigation
Use the following screen navigation commands while viewing large log files.
- CTRL+F – forward one window
- CTRL+B – backward one window
- CTRL+D – forward half window
- CTRL+U – backward half window
Less Command – Line navigation
In a smaller chunk of data, where you want to locate particular error, you may want to navigate line by line using these keys:
- j – navigate forward by one line
- k – navigate backward by one line
Less Command – Other Navigations
The following are other navigation operations that you can use inside the less pager.
- G – go to the end of file
- g – go to the start of file
- q or ZZ – exit the less pager
Simulate tail -f inside less pager – Press F
Once you’ve opened a file using less command, any content that is appended to the file after that will not be displayed automatically. However, you can press F less command will show the status ‘waiting for data‘. This is as similar to ‘tail -f’.
Also, refer to our earlier article about how to view multiple logs files using tail -f.
Less Command – Count magic
Similar to Vim editor navigation command, you can give 10j to scroll 10 lines down, or 10k to go up by 10 lines.
- 10j – 10 lines forward.
- 10k – 10 lines backward.
- CTRL+G – show the current file name along with line, byte and percentage statistics.
Other useful Less Command Operations
- v – using the configured editor edit the current file.
- h – summary of less commands
- &pattern – display only the matching lines, not all.