Combining the GNU super-powers of tail and grep

(or: “Musings on the single-minded, relentless pursuit of perfection”…)

Saving this thought to possibly build upon  for later, adapted from something I just posted to Facebook:

I finally found a question on Stack Exchange that is right square in my wheelhouse, and was able to contribute an answer with which I am completely satisfied, because  I feel it is both better and more thoroughly considered and researched than the existing answers (because it is something I use almost every day, literally; so it is refined over years of usage)! It might seem silly — even useless — to an impartial observer, but I feel like this moment justifies my endless pursuit of perfection, never being satisfied with “good enough” and always chipping away at minor annoyances, trying to improve upon the status quo.

TL;DR: Oh frabjous day; this makes my borogoves all mimsy!
For the record, because one never knows how long a given site will last or when the current content may vanish; here is the content of my SE answer:

I see all these people saying to use `tail -f`, but I do not like the limitations of that! My favorite method of searching a file while also watching for new lines (e.g., I commonly work with log files to which are appended the redirected output of processes executed periodically via cron jobs) is:

`tail -Fn+0 /path/to/file|grep searchterm`

This assumes GNU tail and grep.
Supporting details from the tail manpage (GNU coreutils, mine is v8.22) [] :

> -F same as –follow=name –retry
> -n, –lines=K
> output the last K lines, instead of the last 10; or use -n +K to output starting with the Kth
> If the first character of K (the number of bytes or lines) is a >’+’, print beginning with the Kth item from the start of each file
>,otherwise, print the last K items in the file. K may have a multiplier
> suffix: b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
> GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
> With –follow (-f), tail defaults to following the file descriptor, >which means that even if a tail’ed file is renamed, tail will continue to >track its end. This default behavior is not desirable
> when you really want to track the actual name of the file, not >the file descriptor (e.g., log rotation). Use –follow=name in that case. >That causes tail to track the named file in a way that
> accommodates renaming, removal and creation.
So, the tail portion of my command equates to `tail –follow –retry –lines=+0`, where the final argument directs it to start at the beginning, skipping zero lines.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s