ManInMoon
ManInMoon

Reputation: 7005

How can I tail -f but only in whole lines?

I have a constantly updating huge log file (MainLog).

I want to create another file which is only the last n lines of the log file BUT also updating.

If I use:

tail -f MainLog > RecentLog

I get ALMOST what I want except RecentLog is written as MainLog is available and might at any point only have part of the last MainLog line.

How can I specify to tail that I only want it to write when a WHOLE line is available?

Upvotes: 0

Views: 279

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172788

After many attempts, the only solution for multiple files that worked (fantastically well) for me is the fdlinecombine command. It's a small binary that reads multiple file descriptors and prints data to stdout linewise.

My use case is spawning multiple long-running ssh commands in the background and following their output, without having the lines garbled or interrupted in between.

Upvotes: 1

Dmitri
Dmitri

Reputation: 2748

By default, tail outputs whole lines unless you use the -c switch to count characters. Something like

 tail -n 20 -f MainLog > RecentLog 

(substituting the number of lines you want prepended to the second file for "20") should work as you want.

But if if doesn't, it is possible that using grep to line-buffer your output will fix this condition. See this question.

Upvotes: 1

Related Questions