less
less
and zless
are available in all versions of Linux and almost all of Unix. We can use them to read the log files.
Browse the file
CTRL + F
– move forward one windowCTRL + B
– go back one windowCTRL + D
– advance half windowCTRL + U
– go back half windowj
– advance one line, 10j advance 10 linesk
– go back one line, 20k go back 20 linesG
– end of file10G
– go to line 10 from the endg
– start of file10g
– go to line 10CTRL + G
– show file name, line, byte and percentage of progressq
or ZZ
– exith
– help 🙂
Search and filter
Forward
• /
– search for a pattern
• n
– navigate to the next occurrence
• N
– navigate to the previous occurrence
Backward
• ?
– look for a pattern
• n
– navigate back to next occurrence
• N
– navigate to next occurrence forward
Show only lines that meet a pattern:
&Pattern
To redisplay all lines:
& <Enter>
Bookmarks
When the file is large and we want to mark a position to which we want to return we can use marks.
The marks are identified with a letter. We create them with the m
command and navigate to them with the single quote:
ma
'a
Simulate tail -f
When we read running log files they will continue to increase. We can indicate unless it shows us the last lines with F
. If we want to stop monitoring the new lines, press Ctrl + C
.
Show line numbers
We can open a file showing the line numbers by adding the -N
option. Once the file is open we can add the line numbers by typing -N
and pressing enter.
grep
The grep command allows us to filter text files in this way we can filter log files to keep the lines that may be relevant to us.
Filter lines that do not meet a pattern:
grep -v [\ STDOUT \] logfile.log> logfile_filtered.log
In this case we have created a file with the lines that do not have the text [STDOUT].
head and tail
They allow us to keep the first or last rows of a file.
For example we can keep them with the last lines of the file from a row number. This is valid if we are not interested in the first lines of the file:
tail -n +587040 logfile.log> log_from1500.log
First lines of a file. In the following file we have identified in which line the 16 hours start and we have been left with the one hour logs.
head -n 284523 log_from1500.log> log_from1500to1600.log
References
Unix Less Command: 10 Tips for Effective Navigation