Reading logs with less
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.logto open a log file -
-Sto chop long lines (truncate instead of wrap) -
Gto jump to the end of the file -
page up/bto scroll up -
-Sagain to toggle back to wrapping -
qto quit
To keep an eye on new log entries:
-
less laravel.logto open a log file -
-Sto chop long lines -
Fto start tailing the file (pressCtrl+Cto 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 pressEnterwithout a search term
Follow mode
-
F: follow mode (liketail -f), pressCtrl+Cto 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)