Introduction
In the day-to-day of a system administrator or developer, it is common to need to review the content of text files that can be very large: logs, configurations, source code, or command output. Opening these files with a traditional editor like vim or nano can be cumbersome when you only want to read information without modifying it. This is where the less command comes in, a command-line tool in Linux designed specifically for interactive and paginated text viewing. Unlike more, less allows you to navigate both forward and backward, search for patterns, and mark positions, all without loading the entire file into memory. In this article we will explore its most useful features and how to get the most out of it in your daily workflow.
What is less?
Less belongs to the GNU utilities set and is included by default in almost all Linux distributions. Its name comes from the phrase “less is more”, indicating that it offers more functionality than the classic more command while keeping resource consumption low. When you invoke less followed by a file name, the program loads the file into an internal buffer and displays a screen (or window) of text according to your terminal size. From there you can navigate using the keyboard, perform searches, jump to specific lines, and even open multiple files simultaneously. The key to its efficiency is that it only keeps the visible portion and a few adjacent lines in memory, allowing you to work with multi‑gigabyte files without noticeable lag.
Basic Usage
To start using less, simply type less filename in the terminal. If the file does not exist, less will show an error message and return to the prompt. Once inside the interface, you will see the file’s content starting from the first line. In the lower‑left corner an indicator shows the percentage of the file you have viewed and the current line number. You can quit less at any time by pressing the q key, which returns to the command line without saving changes. If you want to view several files at once, just pass them as arguments: less file1.txt file2.txt, and less will let you switch between them using the :n (next) and :p (previous) commands.
Basic Navigation
Inside less, the movement keys are intuitive for anyone who has used other viewers. The space bar advances a full screen downward, while the b key moves back a full screen upward. To move line by line, use the up and down arrow keys or the k and j keys, respectively, mimicking vi’s behavior. To go to the start of the file, press g; to go to the end, use G (uppercase). You can also jump to a specific line number by typing the number followed by g (for example, 45g goes to line 45). These options make exploring large files fast and precise, without needing to count lines manually.
Searching within less
One of less’s most powerful features is the ability to search for text patterns while viewing the file. To start a forward search, type / followed by the expression you want to find and press Enter. Less will highlight all matches and move the cursor to the first occurrence after the current position. To search backward, use ? instead of /. Once a match is highlighted, you can move to the next match with n and to the previous match with N. If the expression contains spaces or special characters, it is advisable to enclose it in single quotes or use the appropriate escape. Furthermore, less allows the search to express basic regular expressions if the -R option is enabled, further increasing its usefulness for analyzing logs or code.
Marks and jumps
Less also allows you to mark positions within the file to return to them quickly. By pressing the m key followed by any letter (for example, ma) you set a mark named a on the current line. Later, you can return to that mark by typing ‘a (a single quote followed by the letter). This mechanism is useful when you need to compare distant sections of the same document or return to a point of interest after performing a search. Another way to jump is to use the :e command to open a new file without leaving less, and 😡 to exit and save a state file (less common). These features make less a semi‑interactive reading environment that adapts to more complex workflows.
Customization and options
The behavior of less can be adjusted via environment variables or command‑line flags. Some of the most used are -N, which shows line numbers on the left of each screen; -S, which truncates long lines instead of wrapping them, useful for viewing code or logs with very long lines; -i, which makes searches case‑insensitive by default; and -X, which leaves the screen contents in the terminal when exiting, preventing it from being cleared. You can combine these options when invoking less, for example: less -NSi file.log. Likewise, the LESS environment variable allows you to set default values; for example, export LESS=’-NSi’ will cause every invocation of less to inherit those options without having to specify them each time.
Conclusion and alternatives
In summary, less is an essential tool for any Linux user who needs to read large files quickly and safely. Its combination of bidirectional navigation, powerful search, marking, and low memory consumption makes it superior to more
This post is also available in ESPAÑOL.