How to use 'less' command?

The less command is a powerful tool for viewing text files in Linux. Here’s how to use it:

Basic Usage

  1. Open a file:
    To view a file using less, use the following command:
    less filename.txt
    Replace filename.txt with the name of the file you want to view.

While in the less interface, you can use the following keyboard controls:

  • Space: Move forward one page.
  • b: Move backward one page.
  • Enter: Move forward one line.
  • Arrow keys: Navigate up and down line by line.
  • g: Go to the beginning of the file.
  • G: Go to the end of the file.
  • /pattern: Search for a specific text pattern. After typing the pattern, press Enter to find the next occurrence.
  • n: Repeat the last search in the same direction.
  • N: Repeat the last search in the opposite direction.
  • q: Quit less and return to the command prompt.

Example

To view a file named sample-file.txt, you would run:

less sample-file.txt

You can then use the navigation controls to explore the file. The less command is particularly useful for large files, as it allows you to view content without overwhelming your terminal screen.

0 Comments

no data
Be the first to share your comment!