79

I am creating an application that writes to a log file, and I need to know how in Linux / Bash to continuously display the log file to the screen (updating the screen with every new line put into the log).

So as an example, lets say I want to push a running log of apache/error.log to the screen (ssh terminal) continuously updating.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Aaron Murray
  • 1,920
  • 3
  • 22
  • 38

5 Answers5

124

Try the tail command:

tail -f filename
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • 2
    vu well now I feel like an idiot. I use the tail command all the time, (just not with the -f parameter). man tail would have been my friend. Thank you, exactly what I was looking for!! Will accept the answer when it allows (9min) – Aaron Murray May 26 '11 at 19:19
  • vu this is even better than writing a script :) (although could be added to a script I suppose as well) – Aaron Murray May 26 '11 at 19:25
  • 2
    with the option `--follow=name` you'll be sure to keep the content of the log file displayed even if the file has been rotated/recreated by a cron job. – Cédric Julien May 26 '11 at 21:12
  • 2
    @aaron Just a note since you mentioned you are creating the application, and then plan to tail the log, just be careful to understand that just because you have written a line in the application doesn't mean the line has shown up in the log since the output may be buffered. – frankc May 27 '11 at 15:54
32

Another solution is

 less +F filename

or just less filename and typing "F" into it (pressing shift+f). It can be better than tail, because it allows you to cancel continuous printing temporary, go backward to look something and reenable it with "F" (shift+f) again

furins
  • 4,979
  • 1
  • 39
  • 57
osgx
  • 90,338
  • 53
  • 357
  • 513
  • I am taking notes, thats a great alternative! – Aaron Murray May 26 '11 at 19:30
  • Plus, it can truncate long lines **out of the box** for you with the `-S` flag, while allowing you to scroll left/right through them. Superior to the `tail -f file.log | cut ...` method. – sevko Dec 11 '14 at 15:07
  • I don't understand the key combo. First press `F` then `Shift`? Or press them simultaneously? I tried but it made no difference. – a06e Mar 13 '15 at 17:10
  • 1
    becko, thank you, just type "F" (Shift + F) - press and hold Shift, then press f, then release both keys. Edited my answer. – osgx Mar 13 '15 at 17:25
11

The watch command can also be of use.

watch tail logfile

Would show you the last 5 lines of the log file. It can be extended to any command which prints stuff to stdout.

Yes, using tail -f is the traditional solution, but depending on what you are trying to do, this might work better.

Seth Robertson
  • 30,608
  • 7
  • 64
  • 57
6

ssh {remotehost} tail -n0f {logfile}

This will give you zero lines initially, and continuously print any new lines that appear in the file.

bukzor
  • 37,539
  • 11
  • 77
  • 111
6

You can also:

less filename.txt
and press 'F'

has one plus - you can anytime CTRL-C and scroll back in log and start watching again with the 'F'.

clt60
  • 62,119
  • 17
  • 107
  • 194
  • 2
    I am so amazed at the response times on the questions, I tried answsering a couple questions and there are many people that are faster on the refresh button around here than I :) Thanks for the answer / tip as well – Aaron Murray May 26 '11 at 19:31