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?
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
.
-
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? Jan 22, 2014 at 17:20
-
2
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]*://'
-
8
-
cat -n
is likeawk '{print NR,$0}'
, so slightly more lightweight. Mar 16, 2011 at 16:39 -
1I was assuming that systems without
tac
might also lack the BSD-derived version ofcat
(USG-flavoredcat
didn't have-v
,-n
, etc. I think something triggered my "old ****".) Mar 16, 2011 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"?
-
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.– ArcegeMar 7, 2011 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.
-
1The “go to last line” function is also usually bound to
>
(without Meta or ESC) andG
. Mar 8, 2011 at 6:13 -
@Chris, I didn't realize Meta wasn't required in less. But that is was you would use in Emacs. Mar 8, 2011 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