How to Use the Linux Command less and Its 6 Options

The Linux command less is one of the most frequently used commands for reading or viewing the contents of a file. This command allows users to display file contents on the screen in a paginated manner, making it easier to navigate to the desired sections. While similar to commands like cat or more, less offers more features and is especially useful when dealing with large files. In this post, we will explore the basic usage of the less command and various options it provides.

Basic Concept of the Linux Command less

The Linux command less operates by reading only the parts of a file as requested by the user, rather than loading the entire file at once. As the name suggests, less uses minimal memory even when opening large files and operates quickly. Due to these characteristics, less is often used when detailed examination of log files, code files, or other large documents is necessary.

Basic Usage

The basic way to use the less command is as follows:

less filename
ShellScript

After entering the command, the content of the file specified by filename will be displayed on the screen. You can then navigate through the file using the keyboard.

Basic Navigation Keys

  • Arrow Keys (Up, Down, Left, Right): Move up and down one line at a time or scroll left and right.
  • Page Up/Page Down: Move up and down one page at a time.
  • g: Move to the beginning of the file.
  • G: Move to the end of the file.
  • /search_term: Search for search_term in the file.
  • n: Move to the next search result.
  • N: Move to the previous search result.
  • v: Open the file in an editor.
  • q: Exit less.

With these basic key operations, you can sufficiently navigate through a file using less. However, less also offers a variety of options and features beyond these basics.

Useful Options

The less command supports various options to enhance file navigation efficiency. Let’s look at some commonly used options.

-N Option: Display Line Numbers

Using the -N option, you can display line numbers for each line in the file. This feature is particularly useful when analyzing log files or code files.

less -N filename
ShellScript

With line numbers displayed, it’s easier to navigate to a specific line. For example, entering 50G moves directly to line 50 of the file.

Here is an example of output with line numbers using the -N option. It functions similarly to the -n option in the cat command.

Figure 1. Linux Command less: Displaying line numbers with the -N option
Figure 1. Linux Command less: Displaying line numbers with the -N option

-s Option: Squeeze Multiple Blank Lines

The -s option reduces multiple blank lines to a single line. The s stands for “squeeze,” simplifying the viewing of files with many blank lines.

less -s filename
ShellScript

-S Option: Disable Line Wrapping

By default, less automatically wraps long lines that don’t fit the screen onto the next line. However, this can be inconvenient at times. The -S option prevents line wrapping, displaying long lines as a single line.

less -S filename
ShellScript

As shown in the image below, a > symbol on the right side of the screen indicates that there is more content to view. In this case, you can use the left and right arrow keys to scroll horizontally.

Figure 2. Linux Command less: Disabling line wrapping with the -S option
Figure 2. Linux Command less: Disabling line wrapping with the -S option

If you want to search within a file without distinguishing between uppercase and lowercase letters, you can use the -I option. This option makes searches case-insensitive.

less -I filename
ShellScript

For example, searching for /TOTAL will match variations such as Total, TOTAL, and total.

Figure 3. Linux Command less: Performing a case-insensitive search with the -I option
Figure 3. Linux Command less: Performing a case-insensitive search with the -I option

-p Option: Start at a Specific Pattern

If you want to open a file and start viewing from a specific pattern, you can use the -p option. This option searches for the specified pattern as the file opens and displays content from that point onward. This is particularly useful for quickly finding and reviewing error messages in log files.

less -p "search_term" filename
ShellScript

Here’s an example of searching for the word “total” and viewing the content from that point.

less -p "total" example.py
ShellScript

When executed, the location of the specific string is highlighted, making it easy to locate and start reading from the relevant section.

Figure 4. Linux Command less: Finding and displaying content from a specific pattern using the -p option
Figure 4. Linux Command less: Finding and displaying content from a specific pattern using the -p option

+F Option: Real-Time Monitoring (Especially Log Files)

With the less command, you can monitor file changes in real-time. The +F option continuously displays content as it is appended to the end of a file.

less +F filename
ShellScript

This option is mainly used for real-time log file monitoring. It offers similar functionality to the tail -f command but with the added advantage of less’s powerful navigation features. The message “Waiting for data… (interrupt to abort)” appears at the bottom of the screen, indicating that less is waiting for new data. Pressing Ctrl + C stops the real-time monitoring.

Figure 5. Linux Command less: Real-time monitoring using the +F option
Figure 5. Linux Command less: Real-time monitoring using the +F option

Precautions

Here are some things to be aware of when using the less command:

  • File Size: While less operates efficiently in terms of memory usage, opening very large files may still affect performance. Searching within large files may also take some time.
  • Environment Variables: less is influenced by environment variables. For example, you can set default options using the LESS environment variable. Misconfigured environment variables can cause unexpected behavior, so it’s wise to check them beforehand.
  • Non-Editable: less does not allow editing of the file contents. It’s purely for reading and navigating. To modify a file, you should use a text editor like vim or nano.

Summary

The Linux command less is an extremely useful tool for efficiently reading and navigating files in a Linux environment. With its various options and key commands, you can easily manage even large files, and it offers real-time log monitoring as well. By mastering the basic usage and frequently used options, you can significantly enhance your productivity in Linux tasks.

Now that you understand the basic usage of the less command and its useful options, try to apply them actively in your work. When it comes to handling files in Linux, less is sure to become an indispensable tool.

References

Leave a Comment