40

I want to display the last 10 lines of my log file, starting with the last line- like a normal log reader. I thought this would be a variation of the tail command, but I can't find this anywhere.

Yarin
  • 173,523
  • 149
  • 402
  • 512

5 Answers5

58

GNU (Linux) uses the following:

tail -n 10 <logfile> | tac

tail -n 10 <logfile> prints out the last 10 lines of the log file and tac (cat spelled backwards) reverses the order.

BSD (OS X) of tail uses the -r option:

tail -r -n 10 <logfile>

For both cases, you can try the following:

if hash tac 2>/dev/null; then tail -n 10 <logfile> | tac; else tail -n 10 -r <logfile>; fi

NOTE: The GNU manual states that the BSD -r option "can only reverse files that are at most as large as its buffer, which is typically 32 KiB" and that tac is more reliable. If buffer size is a problem and you cannot use tac, you may want to consider using @ata's answer which writes the functionality in bash.

Community
  • 1
  • 1
Rick Smith
  • 9,031
  • 15
  • 81
  • 85
  • I'm sure this is right, but looks like the 'tac' command doesn't exist on OSX shell... – Yarin Nov 05 '11 at 01:38
  • @Yarin See http://stackoverflow.com/questions/742466/how-can-i-reverse-the-lines-in-a-file for some alternatives to `tac`, not all of which are portable. – ephemient Nov 05 '11 at 03:57
  • Sorry, I have to ding you for "useless use of cat" http://www.smallo.ruhr.de/award.html since tail has the option -r to reverse the order. So `tail -r -n 10 ` is the better choice. As a bonus tail -r works on non-GNU systems like OSX, Solaris, AIX, etc. – Bruno Bronosky Dec 03 '14 at 20:45
  • @RichardBronosky Nice link, I've edited my answer to also include the `-r` option. – Rick Smith Dec 03 '14 at 21:52
  • @RickSmith respectable humility, friend. I received the award in 1999 and have been diligent ever since. Also, I was GNU-only from 1995-1999 before getting plunged into Posix systems and had to relearn a lot because the admins refused to install all the sugar I was used to. – Bruno Bronosky Dec 04 '14 at 03:01
  • GNU tail does not have the `-r` option, see https://www.gnu.org/software/coreutils/manual/html_node/tail-invocation.html#index-BSD-tail – m13r Aug 05 '16 at 12:00
16

tac does what you want. It's the reverse of cat.

tail -10 logfile | tac

drysdam
  • 8,341
  • 1
  • 20
  • 23
8

I ended up using tail -r, which worked on my OSX (tac doesn't)

tail -r -n10
Yarin
  • 173,523
  • 149
  • 402
  • 512
  • This is the best answer because it is not GNU specific and avoids a "useless use of cat". http://www.smallo.ruhr.de/award.html – Bruno Bronosky Dec 03 '14 at 20:47
  • 1
    Does not work `root@elk:/# tail -r -n1 /var/log/logstash-test-output.log tail: invalid option -- 'r'` – basickarl Feb 26 '16 at 15:56
  • 2
    @KarlMorrison This won't work for non-BSD tail commands. See [my answer](http://stackoverflow.com/a/8017478/616644) for more help... – Rick Smith Aug 16 '16 at 18:47
3

You can do that with pure bash:

#!/bin/bash
readarray file
lines=$(( ${#file[@]} - 1 ))
for (( line=$lines, i=${1:-$lines}; (( line >= 0 && i > 0 )); line--, i-- )); do
    echo -ne "${file[$line]}"
done

./tailtac 10 < somefile

./tailtac -10 < somefile

./tailtac 100000 < somefile

./tailtac < somefile

ata
  • 2,045
  • 1
  • 14
  • 19
2

This is the perfect methods to print output in reverse order

tail -n 10 <logfile>  | tac
basickarl
  • 37,187
  • 64
  • 214
  • 335