Get 50% Discount Offer 26 Days

Recommended Services
Supported Scripts
WordPress
Hubspot
Joomla
Drupal
Wix
Shopify
Magento
Typeo3
Mastering the less Command in Linux: A Comprehensive Guide with Examples

One of the more powerful and useful Linux Command Line utilities is less command. It is a simple tool that lets you look through large files and command log output right in your terminal without needing a Text editor. This guide will get you up to speed with what can generally be done via the command line, from beginner to advanced techniques, and lots of examples to help you level up your command line expertise.

1. What is the use of less Command?

The less command is a terminal pager program that lets you view files or command outputs one screen at a time. Unlike more, which only allows forward navigation, less supports both forward and backward scrolling, offering greater control. It doesn’t fully load a file into memory, making it an efficient choice for exploring large files.

Why is less command used?

  • View and explore large files without loading them into memory.
  • Scroll forward and backward for more flexible navigation.
  • Search for specific Text within files or outputs.
  • A valuable tool for working with logs, configuration files, and long outputs.

2. Basic Syntax and Options of less command

The fundamental syntax for the less command is:

less [options] filename

Common Options Explained

  • -N: Display line numbers on the left for easier reference.
  • -S: Disable automatic line wrapping for easier horizontal scrolling.
  • -X: Keep the file’s content displayed after exiting less.
  • -F: Exit automatically if the content fits on one screen.

Example of Basic Usage of less command in linux

To open a file named example.txt, use:

less example.txt
Displaying content of example.txt file using the less command in Linux

This command allows you to navigate through the content one screen at a time, helping you easily review even very large files.

3. Navigating with less command in linux

Mastering navigation is key to leveraging less. Here are the main commands to know:

Basic Navigation Keys

  • Down Arrow or j: Scroll down one line.
  • Up Arrow or k: Scroll up one line.
  • Space: Move forward one screen.
  • b: Move backward one screen.
  • G: Jump at the bottom of the file.
  • g: Go to the top of the file.

Jump to Specific Lines using less.

Navigating through large files using the less command can be tiresome; luckily, there’s a simple way to jump to a specific line. Entering a line number followed by g will take you directly to it. In fact, you could type 2g, and it will instantly take your cursor to line 2. It is handy when you’re dealing with very large log files or documents, and you need to jump around. With this slide, people are able to reach a certain data point without having to read the full file, which makes this an extremely powerful and practical command to have in their everyday software.

2g
Jumping to specific lines in a file using the less command in Linux

This command moves you directly to line 2, making it easier to review specific sections in a document or log file.

4. Practical Examples for Beginners

Example 1: Viewing System Logs in linux

It can help us as we navigate through our systems, to know their behavior, and to detect and fix problems. Typically, files like /var/log/syslog are huge and hard to navigate through with just basic commands. They use less, which means that you can open them, scroll through them at your own pace, and search through the files for selected words and timestamps. As a result, it could be a better choice for system administrators or developers that require interaction with logs.

less /var/log/syslog
Viewing system logs in Linux using the less command

With this command, you can search for error messages or monitor logs without loading the entire file into memory.

Example 2: Displaying a File with Line Numbers in linux

When analyzing a file, it’s often useful to display line numbers. Such a feature is especially useful for coding or reviewing configuration files. The -N option simplifies identifying and referencing specific lines while scrolling through content. This feature is helpful for discussing sections of code with colleagues or troubleshooting errors within a script.

less -N example.txt
Displaying a file with line numbers in Linux using the less command

The line numbers will appear on the left side, providing a clear view of your position in the file.

Example 3: Preventing Line Wrapping

Sometimes, files contain long lines of Text that automatically wrap to the next line in the terminal. This format may be challenging to read and understand for log files or structured data. By using the -S option, less prevents line wrapping, allowing you to scroll horizontally to view the full line.

less -S example.txt
Using the less command in Linux to prevent line wrapping

Use the right and left arrow keys for horizontal scrolling. This checks for the original formatting of the content is preserved, which is important for reviewing long commands, complex code, or data tables.

5. Advanced Options and Customizations

Once you’re familiar with basic operations, dive into these advanced options to take your usage of less to the next level.

Option 1: Keeping Content Displayed After Exit

By default, less clear the terminal when you Exit, which can be inconvenient if you need to reference the content. The -X option changes this behavior, allowing the file’s content to stay visible even after you Exit less. Such functionality is useful when you need to continue working with other commands while retaining the displayed data as a reference.

less -X example.txt
Using the -X option in the less command to keep content displayed after exit

This tweak makes it less versatile for multitasking within the terminal.

Option 2: Opening Multiple Files in linux

When comparing or analyzing different files, it’s helpful to open them all in one less session. Such an approach eliminates the inconvenience of constantly switching between different windows or commands. Use:

less file1.txt file2.txt

Within less, navigate between the files using:

  • :n: Move to the next file.
  • :p: Return to the previous file.

Example: Line Numbers with Multiple Files in linux

It’s sometimes convenient to work with multiple Text files in Linux and it could help simplify analysis and referencing when you are viewing and comparing them with line numbers. For this, you’ll use the less command with the -N option. It will allow you to display line numbers for each file, which provides a very handy way of finding specific lines (and their corresponding numbers) or what content lines have in common between files. For example, less -N file1.txt file2.txt simultaneously opens file1.txt and file2.txt in the less interface, showing the line number through the file. Once this feature is in place, file comparison is better, and you can navigate between files easily to improve your productivity when working with large amounts of data.

less -N file1.txt file2.txt
Viewing and comparing multiple files with line numbers using the less command

Displaying line numbers and enabling file switching makes cross-referencing seamless.

6. Using less with Other Commands

The true power of less lies in its ability to work with other commands through pipes. Such a feature allows you to view command outputs more comfortably.

Example 1: Viewing dmesg Output log

The dmesg command outputs kernel messages, which can include detailed system and hardware information. This output can be long, so piping it to less allows you to scroll and search through it interactively.

dmesg | less
Using the less command with other commands to view output logs

This command is invaluable for system administrators and anyone needing to troubleshoot boot issues or monitor system events.

Example 2: Searching Command Output using less in linux

Command outputs like ls can be overwhelming in directories with many files. Using less makes it easier to search for specific files or details:

ls -l /var/log | less
Searching Command Output using less in linux

Press / to initiate a search within less, followed by the search term:

/error
Using less command with /error search, showing Pattern not found message in terminal

Highlighting matches enables navigation using n for the next result and N for the previous one.

7. Tips for Improving Productivity with less

Boost your efficiency by using these less tips:

Customise Search Behaviour

A particularly powerful customization tip, if you’re using it less often, lives in the case sensitivity: It should be made case-insensitive. This kind of functionality makes sure that searches match the keyword and case of the same case, saving time in looking through files with mixed case Text. To get this feature, use the -I option with less. Suppose example.txt we want to search for something in example.txt but not worry about the case in doing so; then less -I example.txt will help us. With logs code or documents coming from different sources, specific capitalization can vary, and this is a very useful option to streamline the search, especially if your logs code or document is varied and inconsistent from source to source.

less -I example.txt
Case-insensitive search in less using -I option, file list example

Such functionality is helpful when searching for terms that may appear in various cases, such as ‘Error,’ ‘ERROR,’ and ‘error’.

Set Default Options with Environment Variables

To save time and avoid repeatedly typing the same options when using less, you can set them as environment variables. This method customizes your less command experience by defaulting to the desired options every time you use it. To do this, add a line such as export LESS=’-N -I -S’ to your shell configuration file (e.g., .bashrc or .zshrc). This particular configuration will display line numbers (-N), enable case-insensitive searches (-I), and prevent long lines from wrapping (-S). Once set, every invocation of less will inherit these options, boosting your productivity and making your work more efficient.

export LESS='-N -I -S'
Setting default less options with environment variables using export command

Now, every time you use less, it will automatically show line numbers, enable case-insensitive searches, and prevent line wrapping.

Combine less with Other Tools.

grep:

Using less with other powerful command-line tools can greatly improve your workflow. One common practice is to use grep to filter specific results and then pipe the output to less for easier navigation. For example, running grep “error” /var/log/syslog | less -N will filter out only the lines containing the word “error” from the syslog file and display them in fewer lines with line numbers. It will allow you to focus on relevant information while still taking advantage of less navigation features like scrolling, searching, and jumping between lines, making log analysis and data review more efficient.

grep "error" /var/log/syslog | less -N
Example of combining 'grep' with 'less' to filter and display specific lines from a log file

Such a feature allows you to focus on lines containing specific keywords while providing line numbers for context.

8. Summary

A useful tool for viewing large files and command outputs is the less command. Less includes powerful navigation from basic features all the way to the advanced ones like searching, opening multiple files, and executing other commands. If you’re logging, reviewing code, or even analyzing some complex data, learning less can improve or boost your command line workflow and productivity.

About the writer

Vinayak Baranwal Article Author

Vinayak Baranwal wrote this article. Use the provided link to connect with Vinayak on LinkedIn for more insightful content or collaboration opportunities.


1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Lifetime Solutions:

VPS SSD

Lifetime Hosting

Lifetime Dedicated Servers