tee command in UNIX is a command line utility for copying standard input to standard output. It supports writing whatever it is given from standard input to standard output and optional writing to one or more files. The command is named after T splitter used in plumbing.
To write to standard output and a file use
tee after a pipe and specify the file or files to write to.
echo 'foo' | tee foo.txt foo cat foo.txt foo
This writes the output of the first command to standard output and to a file called
tee and append to a file rather use the
cat foo.txt foo echo 'bar' | tee -a foo.txt bar cat foo.txt foo bar
As data flows through UNIX pipelines it can be useful to take a snapshot of the state of the data. This can be for debugging purposes or to take a backup.
ls ~/ | tee pipe1.txt | grep ^b | tee pipe2.txt | sort -r
In this example there is a requirement to analyse the HTTP2 traffic going through an nginx server. Using
tee the live logs can be streamed to standard output and also write to a file for further analysis later.
tail -f /var/log/nginx/access.log | grep --line-buffered "HTTP/2.0" | tee -a http2.log
To write to a privileged file
tee may be used as part of a pipe to elevate to sudo permissions.
Suppose we have a
file owned by root. Trying to append to this file as a normal user results in a permissions error.
echo 'foo' >> file zsh: permission denied: file
As part of a pipe
tee can take the input, elevate permissions and write to the file.
echo "foo" | sudo tee -a file
Have an update or suggestion for this article? You can edit it here and send me a pull request.
Listening to BBC Radio with mpv
The BBC publishes high quality 320 kbps HLS AAC streams that can be used to listen to radio from the command-line using mpv. Here are the URLs and some aliases to start listening quickly.
Vim: So long Pathogen, hello native package loading
Vim 8 added a native way to load third-party packages. This means, for me at least, there is no longer any need to use a third party plugin helper like Pathogen.
Linux and Unix more command tutorial with examples
Tutorial on using more, a UNIX and Linux command for viewing the contents of a file or files one screen at a time. Examples of viewing a file, viewing multiple files, searching using regular expressions and opening the file in a text editor.