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?

  • 1
    Does your log file cleaned each time, after shell script run Java process? – c0rp Oct 21 '14 at 11:14
  • I use log4j with append mode to write log file. It rotate after the file dimension raise up to 10MB. – theShadow89 Oct 21 '14 at 15:44

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.