50

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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
c0mrade
  • 934
  • 2
  • 9
  • 13

8 Answers8

65

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.

mattdm
  • 39,535
  • 18
  • 99
  • 133
39

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]*://'
geekosaur
  • 31,429
  • 5
  • 79
  • 58
7

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"?

glenn jackman
  • 84,176
  • 15
  • 116
  • 168
  • 1
    Two 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
2

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.

0

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
jofel
  • 26,513
  • 6
  • 65
  • 92
Jet
  • 101
  • 1
0

If you are writing a nagios check the perl module File::ReadBackwards is useful

jamespo
  • 1,171
  • 7
  • 6
-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
nsantana
  • 99
  • 3
-2

Piping into tac also works, eg:

tail -n 3 /var/log/dmesg | tac
marc
  • 1
  • 1