Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to continuously display a file of its last several lines of contents

Tags:

I am trying to find a Unix command (combination, maybe) on how to continuously display a file of its last several lines of contents. But during this displaying, I want some of the top lines are always displayed on the screen top when the rolling contents reach the screen top.

Is that possible?

  1. Suppose I have file, "job.sta", the first 2 lines are:
    job name, John's job on 2013-Jan-30,... Tab1, Tab2, Tab3 0, 1, 2, 1, 90, 89 2, 89, 23 ... 
  2. This file is on its running, its contents are growing, and I don't know what line it's going to end.
  3. So I want to display (always) the first 2 lines when using tail command, when the updating contents reaches a Unix shell screen top. I am using PuTTY at the moment.

Reference: http://www.unix.com/unix-dummies-questions-answers/172000-head-tail-how-display-middle-lines.html

like image 320
Wen_CSE Avatar asked Jan 30 '13 12:01

Wen_CSE


People also ask

How do I print the last 5 lines of a file?

head -15 /etc/passwd To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file. Try using tail to look at the last five lines of your .

What is the command to display last few lines of a file?

Use the tail command to write the file specified by the File parameter to standard output beginning at a specified point. This displays the last 10 lines of the accounts file.

How do you display the last 10 lines of a file in Unix?

Linux Tail Command Syntax Tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates. Example 1: By default “tail” prints the last 10 lines of a file, then exits. as you can see, this prints the last 10 lines of /var/log/messages.

How do you display last 20 lines in Unix?

To display last 20 lines of a file linux use the tail command. Displays the last 20 lines. The default is 10 if you leave out the -n option.


2 Answers

I use this function all the time to monitor a log file in another terminal window.

tail -f <filename> 

I recommend taking it a step forward to look for particular text in the log. Great if you are only interested in seeing some particular entry being written to the file.

tail -f <filename> | grep <keyword or pattern> 
like image 96
wacko413 Avatar answered Sep 26 '22 01:09

wacko413


This will update every 2 seconds rather than whenever data is written to the file, but perhaps that's adequate:

watch 'head -n 2 job.sta; tail job.sta' 
like image 20
William Pursell Avatar answered Sep 23 '22 01:09

William Pursell