Reading logs with less

Published on Shared on X

If you're reading logs with cat or grep, then it's worth giving less a try. less lets you scroll, search, and filter text files interactively. It comes preinstalled on most Linux distros and macOS.

less was created as an improvement to more, which dates back to 1978. more could only scroll forward, less can scroll both ways (and a lot more).

TLDR

This works well for reading Laravel's log files:

  • less laravel.log to open a log file
  • -S to chop long lines (truncate instead of wrap)
  • G to jump to the end of the file
  • page up / b to scroll up
  • -S again to toggle back to wrapping
  • q to quit

To keep an eye on new log entries:

  • less laravel.log to open a log file
  • -S to chop long lines
  • F to start tailing the file (press Ctrl+C to stop tailing)

Less basics

To open a file, run less laravel.log. When you have a file open, use any of the commands below to navigate and search in the file.

Note: commands below are case-sensitive, so G is Shift+g.

Navigation

  • page down / space: page forward
  • page up / b: page backward
  • arrow down / j: scroll one line down
  • arrow up / k: scroll one line up
  • mousewheel scroll: scroll up and down
  • g: jump to the beginning of the file
  • G: jump to the end of the file

Searching

  • /: regex search
  • / + Ctrl+R: literal search
  • n: next match
  • N: previous match
  • -i: make searches case-insensitive

Filtering

  • &: only show lines matching a regex pattern
  • & + Ctrl+R: only show lines matching a literal pattern
  • &!: only show lines not matching a pattern
  • To clear the filter, type & and press Enter without a search term

Follow mode

  • F: follow mode (like tail -f), press Ctrl+C to stop following

Other useful tricks

  • -S: chop long lines instead of wrapping
  • -N: show line numbers
  • q: quit

Opening multiple files

Less can also open multiple files at once. All settings, searching, and filtering are applied to all open files.

To open multiple log files, run less file1.log file2.log, or run less *.log to open all log files in a directory.

  • :n: next file
  • :p: previous file
  • :x: first file
  • =: show current position info
  • ESC+n: next match (across files)
  • ESC+N: previous match (across files)