Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

continuously print the last line of a file Linux termin

Two questions, but only stuck on one. Feel that I need the first one so someone can help me make sense of it.

4) Use cat and /dev/null to create an empty file.

5) Start a background process that continuously prints the last line of the file created in #4..

So what i did for number 4 was:

cat /dev/null > emptyfile

This created an empty file. Okay so I am happy with that. The next question however confuses me. How can I read the last line of an empty file? Better yet how do I continuously do this? Running it in the background isn't a problem. Anyone have any ideas? We haven't covered scripting yet so I don't think that plays a role. As always, thanks for the help.

like image 911
yaegerbomb Avatar asked May 26 '11 05:05

yaegerbomb


People also ask

How do I print the last line of a file in Linux?

tail [OPTION]... [ 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.

How do I display the last line of a text 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. The tail command continues to display lines as they are added to the accounts file.


2 Answers

Use the UNIX command "tail" with the -f option. That will continuously print out contents from the file to the terminal as it is added to the file.

Example:

tail -f emptyfile

You can terminate the tail process by typing Ctrl + C.

like image 101
Kim Burgaard Avatar answered Oct 27 '22 08:10

Kim Burgaard


doesn't tail -f FILE_NAME help?

like image 38
pistacchio Avatar answered Oct 27 '22 08:10

pistacchio