Tee is a command which reads standard input and writes it both to standard output and one or more files. It is named after the T-splitter used in plumbing. The command is available on most popular operating systems.

Example usages

# Displays and writes the output to file.txt
ls -lah |tee file.txt

# Displays and appends the output to file.txt
ls -lah |tee -a file.txt

# Displays and writes the output to file1.txt and file2.txt
ls -lah |tee file1.txt file2.txt

# tee supports process substitution.
# Displays and writes the output to file.txt and the md5sum to md5.txt
ls -lah | tee file.txt >(md5sum > md5.txt)

Using tee with vim

If you opened a protected file with vim and your current user is missing permissions to write to the file you could use :w !sudo tee %. This will write the file as super user. In this case % is replaced with the name of the current file. The ! means that what comes after is a command which will get the current buffer via standard input.