Skip to content
System

Less under Linux to review a large log and find an error

Avatar illustré de Julien Macé 3 min read
Manchot Linux examinant un long journal système avec une loupe pour illustrer la commande less
Less aide à rechercher et relire un journal existant sans modifier son contenu.

A log file of several hundred megabytes is not read in an editor launched by reflex. When a service refuses to start or an error reappears in /var/log, less allows opening the file, searching for a pattern, and going back without altering the content.

I would prefer it for one-off reading or searching in an existing log. For a stream that continues to run, tail -F is more suitable, especially if the log rotation replaces the file.

Linux penguin examining a long system log with a magnifying glass to illustrate the less command
Less helps to search and re-read an existing log without modifying its content.

Check the file before opening it

Start by confirming that you are reading a text file and that your account has permission to view it. This verification prevents launching a pager on a binary file, an incorrect path, or a log reserved for root.

stat -c '%n : %s bytes, rights %a' /var/log/syslog
file /var/log/syslog

On Debian and Ubuntu, /var/log/syslog is common. On other systems, the log often goes through journalctl. The last boot with journalctl is then a better track than a path copied from another server.

Open the log and find an error

Open the file with line numbers if you need to note a location or compare multiple passages.

less -N /var/log/syslog

In less, type /, enter a pattern, then press enter. To find a network outage, for example, search for connection refused, the name of a service, or an IP address. The n key advances to the next occurrence, N goes back to the previous one. g brings you to the beginning, G to the end, and q quits the viewer.

To jump directly to a pattern, use +/ at launch. Quotes protect spaces for the shell.

less -N +/"connection refused" /var/log/syslog

This command does not modify the log. It also avoids printing thousands of lines in the terminal, then losing context at the moment the error appears.

Follow a file or maintain a position

The F key places less in follow mode at the end of the file. Rather launch this variant if the service is still writing to the log:

less +F /var/log/syslog

Press Ctrl+C to stop following and resume navigation in the content already received. You can then search, check the lines just before the error, and then return to following with F.

There is an important limitation: less +F follows the descriptor of the opened file. After a log rotation, it may still be on the old file. To monitor a log that will be renamed and then recreated, use tail -F. For kernel messages, also look at dmesg and kernel logs.

Choose less, cat, or tail based on the diagnosis

  • less is suitable for a large existing file, when you need to search, go back, and re-read a passage.
  • cat remains acceptable for a small configuration file whose output fits in your terminal.
  • tail -F is suitable for an active stream and logs that may be replaced by logrotate.

For a configuration file, often start by identifying its format with file, then open it with less. Only switch to an editor after identifying the line to change and preparing a backup.

Final check before leaving

Before concluding that a service has failed, note the found pattern and re-read about ten lines around it. A simple ERROR may be the result of a previous mistake. The man page less(1) also documents reverse search with ? and the options available on your distribution.

This method keeps the diagnostic reversible: you read, you search, you check the context. No writing goes to the file until you have a precise cause to correct.

Share Facebook X WhatsApp LinkedIn Pinterest Reddit Email