Is there a linux command or some way to look at logs from bottom up rather than from top towards bottom. I know about tail -n <number of lines>, but is there something that I can actually scroll and go from bottom up?
- 807,993
- 194
- 1,674
- 2,175
- 934
- 2
- 9
- 13
8 Answers
I think less +G is what you're looking for — it'll load up the file at the end, and you can use the up arrow (or k) to scroll back. Within the viewer, g will take you to the top of the file and G will take you back to the end. And F will scroll to the end and try to keep reading, like tail -f.
- 39,535
- 18
- 99
- 133
-
n1 +1 :) If you'll be so kind: Can you explain how to search the file from bottom up when using less with +G flag? – Oleg Belousov Jan 22 '14 at 17:20
-
2@OlegTikhonov Sure. `?` is used to search backwards in the file. – mattdm Jan 22 '14 at 17:36
Some systems have tac, which is a whimsically-named backward cat. Without that, you can still do something like
awk '{print NR ":" $0}' $file | sort -t: -k 1nr,1 | sed 's/^[0-9][0-9]*://'
- 31,429
- 5
- 79
- 58
-
8
-
`cat -n` is like `awk '{print NR,$0}'`, so slightly more lightweight. – glenn jackman Mar 16 '11 at 16:39
-
1I was assuming that systems without `tac` might also lack the BSD-derived version of `cat` (USG-flavored `cat` didn't have `-v`, `-n`, etc. I think something triggered my "old ****".) – geekosaur Mar 16 '11 at 16:47
The tac command is like a reverse "cat", if that's what you're after.
If you just want to view a file by starting at the bottom, why don't you use an editor or "less"?
- 84,176
- 15
- 116
- 168
-
1Two problems I see with using editors are paginators: a) not all editors behave properly to network disconnects, meaning that the editor stays around even when the shell sends SIGHUP; b) many editors touch the directory (by creating a swap/temp file), which is disadvantageous if you want to keep the mtime of the directory stable. – Arcege Mar 07 '11 at 17:47
You can run less and then use M-> (that's the meta key, usually alt, and the '>' at the same time) to go to the bottom of the file.
less supports scrolling.
- 121
- 3
-
1The “go to last line” function is also usually bound to `>` (without Meta or ESC) and `G`. – Chris Johnsen Mar 08 '11 at 06:13
-
@Chris, I didn't realize Meta wasn't required in less. But that is was you would use in Emacs. – Carlos Rendon Mar 08 '11 at 17:37
I use this script to read a file from bottom upward
#!/bin/bash
echo -n elpmas.file # resulting file
ctr_line=0
while read line; do
let ctr_line++
tail -n $ctr_line | head -n 1 >> elpmas.file
done <sample.file
if sample.file contains
1
2
3
the result elpmas.file will be
3
2
1
Soluction: Combine tac with less
tac $@ | less
Install
sudo bash -c 'echo "tac \$@ | less" > /usr/local/bin/tacless'
sudo chmod +x /usr/local/bin/tacless
Usage
tacless /var/log/auth.log
- 99
- 3