Overview
The less command is a “pager” used to view the contents of a text file. Unlike the cat command, it does not read the entire file at once. It only displays the parts you are viewing. This means you can open very large log files (even several GB) instantly without consuming significant memory.
In Linux system administration, it is an essential tool for investigating logs, checking configuration files, and scrolling through command results. This article covers basic viewing, efficient navigation/searching, and how to use it with pipelines.
Specifications (Arguments/Options)
Syntax
less [options] [filename]
Alternatively, you can receive output from another command using a pipe:
[command] | less [options]
Main Options
The following table summarizes frequently used options in practical work: | Option | Description | | :— | :— | | -N | Displays line numbers at the beginning of each line. | | -s | “Squeezes” multiple consecutive blank lines into a single line. | | -S | Displays long lines without wrapping (allows horizontal scrolling). | | -i | Ignores case sensitivity during searches. | | -p [pattern] | Opens the file and immediately searches for the specified pattern/string. | | -f | Forces open special or binary files. | | -x [num] | Sets the tab width to the specified number (e.g., -x 4). |
Internal Commands (Keys)
These are the primary keys used for navigation after starting less. | Key | Action | Description | | :— | :— | :— | | Space / f | Page Down | Scrolls down by one full screen. | | b | Page Up | Scrolls up by one full screen. | | Enter / e | Line Down | Scrolls down by one line. | | y | Line Up | Scrolls up by one line. | | d | Half Page Down | Scrolls down by half a screen. | | u | Half Page Up | Scrolls up by half a screen. | | g | Top | Moves to the very first line of the file. | | G | Bottom | Moves to the very last line of the file. | | / | Forward Search | Enter forward search mode (type word and press Enter). | | ? | Backward Search | Enter backward search mode. | | n | Next Match | Moves to the next search result. | | N | Previous Match | Moves to the previous search result. | | = | File Info | Shows the filename, line count, and byte position. | | v | Edit | Opens the file in the default editor (vi, nano, etc.). | | ! [command] | Run Shell | Executes a shell command (e.g., !ls -l). | | q | Quit | Exits less and returns to the shell. |
Basic Usage
Viewing Log Files
The most basic way to use it is to specify the target file as an argument.
less /var/log/syslog
Example output:
Jan 15 10:00:01 ubuntu-server CRON[12345]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Jan 15 10:05:01 ubuntu-server systemd[1]: Starting Cleanup of Temporary Directories...
Jan 15 10:05:01 ubuntu-server systemd[1]: systemd-tmpfiles-clean.service: Succeeded.
Jan 15 10:05:01 ubuntu-server systemd[1]: Finished Cleanup of Temporary Directories.
... (Press 'q' to exit)
Practical Commands
1. View with line numbers and squeezed blank lines
When viewing source code or unformatted logs, combining line numbers (-N) and blank line compression (-s) significantly improves visibility.
less -Ns application_debug.log
2. Check command output with a pager
By piping large command outputs to less, you can easily scroll through the results. Below is an example of viewing loaded kernel modules.
lsmod | less
Example output:
Module Size Used by
nls_utf8 16384 1
isofs 49152 1
vboxsf 45056 0
snd_intel8x0 45056 2
snd_ac97_codec 135168 1 snd_intel8x0
... (Scroll with arrow keys or Space)
3. Open at a specific search position
To investigate a specific error immediately, use the -p option to specify a search keyword.
# Start the display from the first occurrence of "ERROR"
less -p "ERROR" /var/log/nginx/error.log
Customization Points
Modify these parameters based on your environment or requirements:
- File Path (
/var/log/...): Replace with the actual path of the log or configuration file you want to view. - Options (
-N,-s,-S): Use-Nfor line numbers when reading source code. For logs with long lines (such as JSON), use-S(no wrapping) to keep columns aligned. - Search Word (
-p "..."): Change this to the error code or keyword you need to investigate.
Important Notes
- How to Exit: This is a common point of confusion for beginners. You must press the q key to exit (Ctrl+C will not terminate the process).
- Viewing Binary Files: Opening binary files (such as images or executables) with
lessmay show control characters and garble the display. If you open one by mistake, press q immediately. - Buffer with Pipes: When using
command | less, the display might not start until the source command finishes or the output buffer is full. - Editing Feature: Pressing v starts an editor, but the
EDITORorVISUALenvironment variables must be configured first. Usually,viornanowill launch.
Advanced Application
Run shell commands while viewing
You can execute shell commands without closing less by using the ! internal command. Steps:
- Type
!whilelessis active. - A
!prompt will appear at the bottom. Type your command (e.g.,pwd). - Press Enter to execute.
- When prompted “Press RETURN,” press Enter to return to the file view.
Example (inside less):
!pwd
/home/user/projects/logs
!done (press RETURN)
View multiple files sequentially
You can specify multiple files as arguments and switch between them using :n (next file) and :p (previous file).
less error.log access.log system.log
Conclusion
The less command is an essential tool for Linux users, providing powerful searching and scrolling capabilities beyond simple file viewing.
Caution: Use the q key to exit and avoid opening binary files. Mastering basic scrolling with the Space key and searching with / will greatly increase your productivity in terminal environments.
Ideal Use Cases: Log analysis, reviewing config files, and managing large command outputs.
Key Settings: Adjust options like line numbers (-N) or line wrapping (-S) according to the file type.
