84

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.

1

5 Answers 5

129

Try the tail command:

tail -f filename
Sign up to request clarification or add additional context in comments.

4 Comments

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)
vu this is even better than writing a script :) (although could be added to a script I suppose as well)
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.
@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.
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

4 Comments

I am taking notes, thats a great alternative!
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.
I don't understand the key combo. First press F then Shift? Or press them simultaneously? I tried but it made no difference.
becko, thank you, just type "F" (Shift + F) - press and hold Shift, then press f, then release both keys. Edited my answer.
12

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.

1 Comment

more notes. Linux = a million ways to skin the same cat :) thank you
7

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'.

1 Comment

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
6

ssh {remotehost} tail -n0f {logfile}

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.