cheat.sheets:tee
# Display `ls` output to the user, but also write it to the given file.
ls | tee outfile.txt
# As above, but append the data; previous file's data remains intact while
# new data is added at the end of the file.
ls | tee -a outfile.txt
# Pipe the standard output of a given command into `tee`, which then displays
# it to the user and sending the data to files `one`, `two`, and `three`.
[COMMAND] | tee one two three
# Workaround to output data to a file, with root privileges.
echo 3 | sudo tee /proc/sys/vm/drop_caches
# Pipe the current Vim buffer to a shell process, which in this case is `tee`.
# This is especially useful as a shortcut added to `.vimrc` or similar.
:w !sudo tee %
cheat:tee
# To tee stdout to <outfile>:
ls | tee <outfile>
# To tee stdout and append to <outfile>:
ls | tee -a <outfile>
# To tee stdout to the terminal, and also pipe it into another program for further processing:
ls | tee /dev/tty | xargs printf "\033[1;34m%s\033[m\n"
tldr:tee
# tee
# Read from standard input and write to standard output and files (or commands).
# More information: <https://www.gnu.org/software/coreutils/tee>.
# Copy standard input to each file, and also to standard output:
echo "example" | tee path/to/file
# Append to the given files, do not overwrite:
echo "example" | tee -a path/to/file
# Print standard input to the terminal, and also pipe it into another program for further processing:
echo "example" | tee /dev/tty | xargs printf "[%s]"
# Create a directory called "example", count the number of characters in "example" and write "example" to the terminal:
echo "example" | tee >(xargs mkdir) >(wc -c)
$
cheat.sh