Wednesday, October 6, 2010

Using "less" to view server logs

To view logs and search for any pattern, less would be great tool to use.
If logs are archived using pid or date (refer post http://lynuxkid.blogspot.com/2010/10/to-run-process-at-background-and.html for more info)

use "ls -ltr *pattern*" to get latest logs (if many instances logs are in same directory)
$ ls -ltr *PQA1logs*
-rw-r--r--   1 wlsadmin atg      16455232 Sep 28 17:15 PQA1logs.txt.4728104734
-rw-r--r--   1 wlsadmin atg      17439042 Sep 29 08:44 PQA1logs.txt.1628171655
-rw-r--r--   1 wlsadmin atg      20182090 Sep 29 16:31 PQA1logs.txt.4629084624
-rw-r--r--   1 wlsadmin atg      26107020 Oct  1 15:04 PQA1logs.txt.3329163306
-rw-r--r--   1 wlsadmin atg      41899784 Oct  6 13:31 PQA1logs.txt.1501171516

then use less -i (i is to ignore case while searching)
$ less -i PQA1logs.txt.1501171516
Keybindings for less will be similar to Vi (refer post http://lynuxkid.blogspot.com/2010/10/vi-commands.html for Vi commands)

Traversing file
G - To go to end of the file
g - to go to start of the file
j - down one line
k - up one line
space - go forward one page
b - go back one page
F - to refresh page to get latest logs (similar to tail)
Ctrl+c to cancel any running operation for example - to cancel refresh of page.
:100 jump to 100th line

Forward search
/pattern - to search for pattern
n - to search next match
N - to search previous match

Backward search
?pattern - to search for pattern
n - to search previous match
N - to search next match

For more info
h - Summary of less commands.

For gunzip files with .gz - use gzless with above options.
Use gzgrep to grep in .gz file.

1 comment: