Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watch file output in bash terminal as it is being written to the file

Tags:

linux

bash

I am asking this question because I remember very clearly having seen someone I worked with do this before I had a very good understanding of linux and the command line.

I am looking for a way to have the content of a file that is being updated sent directly to a terminal rather than simply logged. The closest approximation I have to this is using watch with tail. What I would like is to have the updates written directly to the terminal at the same time the file is updated.

Anyone seen something like this?

like image 564
ftherese Avatar asked Aug 31 '14 20:08

ftherese


People also ask

How do I display the content of a file as it grows Linux?

The simplest way to view text files in Linux is the cat command. It displays the complete contents in the command line without using inputs to scroll through it. Here is an example of using the cat command to view the Linux version by displaying the contents of the /proc/version file.

Which command displays output on screen as well redirect output to other file?

The '>' symbol is used for output (STDOUT) redirection. Here the output of command ls -al is re-directed to file “listings” instead of your screen. Note: Use the correct file name while redirecting command output to a file.

How will you monitor file change in Linux?

In Linux, we can use the inotify interface to monitor a directory or a file. We do this by adding a watch to the directory or file.


3 Answers

I often redirect both stdout and stderr of some command, e.g (in abatch job)

make >& _make.out

then in another terminal, I can run

tail -f _make.out

it will continuously display the last lines of _make.out, so I'll get it shown in the terminal.

like image 139
Basile Starynkevitch Avatar answered Sep 29 '22 17:09

Basile Starynkevitch


Use the command tee - from it's man page,

Synopsis

  tee [OPTION]... [FILE]...

Description

  Copy standard input to each FILE, and also to standard output.

Or, you could run tail -f on the written file to watch as it is being written (-f is follow).

like image 45
Elliott Frisch Avatar answered Sep 29 '22 17:09

Elliott Frisch


The tee program does exactly what you want. It reads from stdin and displays the data on the terminal while redirecting it to a file a the same time. It is an old UNIX tool and also available from GNU coretutils.

Redirect output of process to a file and display it on terminal at the same time:

process | tee output.file

If you want to append to output.file use the option -a:

process | tee -a output.file
like image 32
hek2mgl Avatar answered Sep 29 '22 19:09

hek2mgl