Viewing files or logs with Linux less command

Main picture of the post: Viewing files or logs with Linux less command

One of my favourite Linux commands is less which is also the one that I use the most when the troubleshooting activities needs to review some sort of logs, config files, audit files and so on. Thanks to this great and simple command present in most of the Linux distributions, it is always easy to have a quick look no matter how big the file size is, because unlike other text editors in Windows (Notepad or Notepad++) it doesn’t load the whole file before presenting it to the viewing or editing tool. That’s why you can quickly check any file because it’s really fast and the option commands that less brings, could help you a lot when searching some particular patterns or message in the file.

However, at the beginning, if you are starting to use it for the very first time it could be a little bit cumbersome like when using for the first days the vi editor. But once you are familiar with less, everything will go really smoothly.

I hope with this post that you can learn about this command, if you never use it before or you don’t use it frequently, so you can start taking advantage of it.

The less command syntax

You can run the command by following this syntax:

less [OPTION]… FILES

Here are some of the command options explained that will be used in the next sections’ examples:

-g : If there are multiple matches on file, just highlight the last one.

-I : Ignore case in all searches

N : Show the line numbers.

-S : Truncate long lines in the view instead of wrapping them

-X : leave the current lines of the view when quitting less

As other Linux commands, less allows you to combine different options to get the desired results.

Despite that you can only interact with one file at the same time, the less command admits multiple files in the same command line. However, you can switch the view from one file to another whenever you want.

The less commands

This section will explain the commands inside less when viewing a file like searching patterns or words or update the content of the file and following it like the command “tail -f“.

First, open a file, in this case the “syslog” file, with the command:

$ less /var/log/syslog

You’ll see a view similar to the following picture and will stay the same unless you interact with it:

If you run less command without any option, it will show the firsts lines of the file. Once inside the less view, you can do the following actions by pressing:

  • “q” to exist the less command
  • The arrow keys to scroll the file
  • “Page up” or “f” to scrolling up the file by page size
  • “Page down” or “b” to scroll down the file by page size
  • “g” go to the first line of the file
  • “G” go to the last line of the file
  • “:n” switch the view to the next file
  • “:p” switch the view to the previous file
  • “v” edit the file with the editor command in $VISUAL or $EDITOR

By typing “F”, the view will update the file continuously with the new content added to the file if any, and follow it (similar to tail -f):

view of the less command in F mode. It is similar to tail -f

To exit this follow mode press Ctrl+C.

You can also do search operations inside the file while viewing a file with less:

  • “/pattern” search the pattern, word, or text on the file forward
  • “?pattern” search the pattern, word, or text on the file backwards
Searching forward with less the word or pattern cron

If there are matches while searching forward or backward, less will jump directly to the first line matched. After the jump, you can look for the next lines that the pattern matched by pressing “n” until it reaches the top or the bottom of the file (Depending on the search direction):

less displaying that there are no more matching lines: Pattern not found
If there are none or no more matching results then less will show “Pattern not found”

A part of searching forward or backward a pattern, it is possible to search in all the file an print only the lines that matches in the less command view by typing “&pattern”

less searching with & a pattern and getting only the lines that matched.

It is even possible to spawn a process from less command, while viewing a file, and run some bash commands by typing “!command”:

less command allows you to run bash commands.

You will leave the less view while keeping the process in the background in order to run the commands, showing the output and prompting you to press RETURN to bring back the less view:

The results of running the previous command in less

These are some of the most common less commands while viewing a file. As you may see, it’s pretty simple to start using it and will not need too much time to get familiar with.

Running less command with options

In addition to the commands described above, it is possible to run less with options in order to leverage its behaviour.

By running less with “-g”, the pattern search will only highlight the current pattern matched.

$ less -g /var/log/syslog
In red, the pattern matches but is not highlighted in white because is the next match in the search.
In red, the pattern matches but is not highlighted in white because is the next match in the search.

You can also change the behaviour of the search by running less with the case insensitive option “-I”:

$ less -I /var/log/syslog
Searching "/cron" with "-I" option, less will highlight both upper and lower words.
Searching “/cron” with “-I” option, less will highlight both upper and lower words.

Showing the line numbers of the file being viewed:

$ less -N /var/log/syslog
Line numbers on the left.

You may chose to run less command with the truncate “-S” option avoiding a file line to occupy two or more lines of the view:

$ less -S /var/log/syslog
running less with -S option
Running less with “-S” option. You will need to scroll in order to view the whole long line

If you choose to run less without the “-S” option, the result could be not comfortable for viewing:

less file viewing without "-S" option
less without “-S” option.

An interesting option for less is to leave the last view instead of cleaning it. This could be helpful to get the traces on the output and use them for analysing other files.

$ less -X /var/log/syslog
Searching with "&cron" and exiting to leave on display the lines matched.
Searching with “&cron” and exiting to leave on display the lines matched.

Viewing the output of a command or script using less

It is not necessarily to run less with a file but with the output of a command using a pipe:

$ grep -i cron /var/log/syslog | less
Viewing the output of a command or script using less