Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Linux 'less' command

June 6, 2024

Using the Linux ‘less’ Command: A Comprehensive Guide

As programmers, we often find ourselves working with large files or browsing through lengthy logs. In such scenarios, using the less command in Linux can prove to be incredibly handy. less allows us to view and navigate through files efficiently. In this blog post, we will explore the various features and functionalities of less, along with examples to illustrate their usage.

Basic Usage

The simplest way to use less is by running it followed by the name of the file you want to view, like this:

1
less myfile.txt

Once you are inside less, you can navigate through the file using the arrow keys, page up/page down, or the spacebar to scroll. To exit less, simply press q.

Searching within Files

One of the key features of less is the ability to search for specific content within a file. To search, press /, followed by the search term, and then hit enter. For example, to search for the word “error”, type /error and press enter. To move to the next occurrence, press n, and to move to the previous occurrence, press N.

1
2
less myfile.txt
/error

less also allows you to jump directly to a specific line in a file. To do this, press : and enter the line number. For instance, to go to line 150, type :150 and hit enter.

1
2
less myfile.txt
:150

Handling Large Files

When working with extremely large files, less offers features to optimize performance. One such feature is the ability to open files in “read-only” mode, which prevents less from trying to write back to the file.

1
less -R myfile.txt

Additionally, less loads files lazily, meaning it only reads a portion of the file at a time, making it ideal for handling large files without exhausting system resources.

Syntax Highlighting

By default, less does not provide syntax highlighting. However, you can enable it by using the -R flag combined with the source-highlight command, which provides syntax highlighting for various programming languages.

1
2
sudo apt-get install source-highlight
less -R myfile.py   # Replace with the appropriate file extension

Editing Files with ‘less’

Although primarily a viewer, less also allows basic editing capabilities. Press v while viewing a file with less to open it in your default text editor, allowing you to make changes and save the modifications.

Customization

You can customize the behavior of less using command line options or by modifying the environment variable LESS. For instance, you can set the number of lines to scroll by default, enable line numbers, or specify a custom prompt.

1
export LESS="-i -N -Pscroll %n (line %m of %t)"

Conclusion

The less command is a versatile tool that allows us to view, search, and navigate through files efficiently. It provides numerous features to enhance productivity when dealing with large files or examining logs. By mastering the various functionalities of less, you can streamline your workflow and improve your programming experience.

Remember to consult the man page for less (man less) to explore more advanced features and options. Happy coding!


➡️ Building Custom Web Interfaces for ESP32: Using Embedded Web Servers


⬅️ Time-Sensitive Networking (TSN) on ESP32: Achieving Deterministic Behavior


Go back to Posts.