Logging Messages
Sometimes, it's beneficial to send output both to the monitor and to a file for logging. Instead of having to redirect output twice, you can use the special tee
command.
The tee
command is like a T-connector for pipes. It sends data from STDIN
to two destinations at the same time. One destination is STDOUT
. The other destination is a filename specified on the tee
command line:
tee filename
Because tee
redirects data from STDIN
, you can use it with the pipe command to redirect output from any command:
$ date | tee testfile
Sun Oct 19 18:56:21 EDT 2014
$ cat testfile
Sun Oct 19 18:56:21 EDT 2014
$
The output appears in STDOUT
and is written to the file specified. Be careful: By default, the tee
command overwrites the output file on each use:
$ who | tee testfile
rich pts/0 2014-10-17 18:41 (192.168.1.2)
$ cat testfile
rich pts/0 2014-10-17 18:41 (192.168.1.2)
$
If you want to append data to the file, you must use the -a
option:
$ date...