Ending tail -f after print n lines

I have the following.

  1. A Java process writing logs on file
  2. A shell script starting the Java process.

I need to read the log file after start Java process to check correct starting.

I have try with tail -f but it remain append forever. I need tail stop after print n lines. There is a way like -n option for previuos lines?


You can pipe the output of tail -f to head to limit the amount of lines shown:

tail -f [PATH] | head -n 100

to only show 100 lines in total.


New lines only

With the plain tail -f, the first 10 lines are from the file as it already exists:

tail -f file.log | head -30

writes 10 lines of log.txt when it is run, and 20 (n-10) lines that are added later.

With a log file, you normally use -f (--follow) to see the lines written in the future.

To see only the 30 lines that where written after tail was started, limit the initial output to 0:

tail -f -n0 file.log | head -30