We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

ELLIOTTCABLE • 10 months ago

I wanted the few remaining advantages of tail -f without leaving my beloved less; so I finally knuckled down and wrote a lightweight, portable sh function to handle the downsides - the key is to use tail to do the file-reading (to interleave multiple files and avoid reading hundred-gigabyte log files into memory), redirect into a temporary file, and then less +F that tempfile.

Here's my robust version; it also moves the filenames from interleaved (as tail -f prints them) up to the start of the logline. To avoid buffering, you'll need either unbuffer from 'expect', or stdbuf from 'coreutils' (although the function works without either):

https://gist.github.com/ELLIOTTCABLE/1c5afda811c1c4b822cf75361e8ded2f

tometchy • 5 years ago

Great tip, thanks :)

chebbytonio • 5 years ago

Thank you. Very usefull.

Nick Enchev • 6 years ago

Super helpful

Mario • 6 years ago

Wow... i found this article today and hell yeah this rocks!

RajaRaviVarma • 6 years ago

Ah! This is awesome.

skovtunenko • 6 years ago

That's a great article!
Personally, I'm stick to the lnav tool (http://lnav.org/)
That's brilliant tool to dig into logs.

Travis Loyd • 7 years ago

Could you update with a comparison to "tailf" which can watch files without causing disk usage in the way "tail -f" does? Does "less +F" also avoid the disk usage?

Seiyeon Kim • 7 years ago

Hi Brian Storti.
I got a lot of good information.
I am working in Korea.
Can I translate and write on my company's tech blog?
Of course, I will reveal the source.
Thanks.

brianstorti • 7 years ago

Sure, no problem.

ShitJustGotReal • 7 years ago

Actually found it really useful, especially the comparison to 'tail -f' and the recommended use cases for each command. Thanks!

Garnett D. • 8 years ago

Great points shared in your post. Thanks for sharing this.

Ralph • 8 years ago

Very educative post. Thanks for taking the time to share this.

Mayk Brito • 8 years ago

less do not pipe like this:
less +F /var/log/exim_mainlog | grep "something"

tail will, tail win

Timo Ylhäinen • 8 years ago

Agree

Markus • 8 years ago

&something in less (can be combined with +F as well)

Maxim Egorushkin • 9 years ago

Open the file in emacs. Execute auto-revert-tail-mode (if it is not your default). Done.

stairbob • 7 years ago

OK, I tried that. Now how do I exit emacs?

dany • 9 years ago

Great article, thanks! http://histoire-bateaux-aviron.fr/

依云 • 9 years ago

This will cause disasters for many cases for me, because I usually tail -f large log files.

Alex • 10 years ago

less -W +F somelogfile

Using -W highlights the first (unread) new line after any forward movement such as scrolling to the next page.

paulfeakins • 10 years ago

Great article, thanks!

paulfeakins • 10 years ago

Fantastic article, thanks!

Stefan Seidel • 10 years ago

And also one should use -n with less, because counting lines in a 3GB log file could take more time than you want.

Peter Eisentraut • 10 years ago

I switched back from less to tail recently as the default tool because tail is less prone to get messed up by terminal issues.

brianstorti • 10 years ago

What do you mean by "terminal issues"?

Peter Eisentraut • 10 years ago

Sometimes, when you are logged in remotely somewhere, and you scroll around in less, "weird things" happen. The cursor jumps around, key presses are not recognized properly, or something like that. It doesn't happen very often, but even 1 out of 100 made me annoyed enough that I went back to the simpler tool by default.

brianstorti • 10 years ago

hm, it never really happened to me, but it's good to know. Thanks for sharing.

Andrew Dixon • 10 years ago

This is very cool. One issue I haven't resolved yet is that tailing (or should I say lessing) my rails development log is kind of wonky because less isn't handling the colors very well. I haven't tried to debug it yet but that would be a problem.

Andrew Dixon • 10 years ago

OK, 2 seconds of googling and I found the -r flag:

less -r +F log/development.log

-r tells less to always try to interpret color output.

brianstorti • 10 years ago

To make things easier, you can also define the 'LESS' environment variable with some default options. e.g.: echo "export LESS=-R" >> ~/.bashrc"

Pádraig Brady • 10 years ago

Note tail -f will be more responsive (due to using inotify), but more importantly tail supports a -F mode which is what you probably want by default so that rotated log files are followed

brianstorti • 10 years ago

You can use --follow-name with less. About the responsiveness, it was never really an issue for me, less' pooling works well enough for my needs, but if that's not your case, as I said, use the best tool for the job at hand.

Gonzih • 10 years ago

I have same behavior in tmux+tail. Also please don't forget about ctrl+s/q in tail.

Christoph • 10 years ago

This is great, but if I want to execute this on a remote machine via a script (I'm using tail -f for viewing that machine's logs right now), it does not register the +F part... :-( any suggestions for that?

miglen • 10 years ago

This is really helpful, I'm using tailf all day long and this is cool.

Brett Wilkins • 10 years ago

Another important consideration would be the slightly different `tail -F` which will survive log rotations.

brianstorti • 10 years ago

Check out less' --follow-name

Orphi • 10 years ago

You cours use multitail which is Nice to use

binaryphile • 10 years ago

If you use a screen multiplexer such as tmux, you can also delegate the scrolling and searching functions to it, allowing you to go back to basic tools such as cat and tail.

megmondo_pal • 10 years ago

tail -F

Gastove • 10 years ago

Dig this! Question: any idea about the memory use of `less +F` vs `tail -f`? Little nervous about `less` keeping a couple gigs of logs in working memory.

Robert Thille • 10 years ago

Or, you could run tail -f in your shell mode in emacs, and then use your normal keybindings to search...

pfix • 10 years ago

well, the biggest + for tail is: you can prese enter to insert empty lines on the screen, thenyou trigger a action and you can quickly see where the logs of your action started

jesse • 10 years ago

don't tell me how to run my life, devil woman!

machikakara • 10 years ago

Maybe i am wrong but with less cant you pipe data into it so u can solve the other issue with multiple files like.

tail -f * | less +F

i don't have a shell in-front of me to test but if i am wrong please feel free to correct me.

Mark R Lindsey • 10 years ago

Mac users: iTerm2 lets you scroll back indefinitely, and regex search the buffer. So if you find yourself in tail -f, then you can still find your way around.

XANi • 10 years ago

when I do tail -f |less, that makes changing it to tail -f | grep or tail -f | awk sth faster. That is why people use it

John • 10 years ago

For random browsing less +F is nice, and is my first use for seeing logs. However, most of the time the logs are way too verbose, so 'tail -f foo.log | grep bar | grep -v baz.com' is what I usually end up creating in short order.

tldr; you need to know both

Guest • 10 years ago

And when you're already in less, you can turn on follow mode with F.