tee Linux Command
Reads from stdin and writes to both stdout and one or more files simultaneously.
What does tee do?
Reads from stdin and writes to both stdout and one or more files simultaneously.
Syntax
command | tee filenamePractical example
echo 'hello' | tee output.txtQuestions about tee
How do I save output to a file and display it?
Use `command | tee filename` to write output to a file while still displaying it on the screen. This is useful for logging.
How do I append to a file with tee?
Use `command | tee -a filename` to append output to a file instead of overwriting it. The `-a` flag means append.
How do I write to multiple files with tee?
Use `command | tee file1 file2 file3` to write the same output to multiple files simultaneously.
How do I use tee with sudo?
Use `command | sudo tee filename` to write to files that require root permissions. This avoids needing to run the entire command as root.
How do I see what tee is writing?
tee displays everything on stdout by default, so you can see the output while it is being written to the file.
How do I use tee in a script?
Use `echo 'log entry' | tee -a logfile.log` in scripts to both log messages and display them on the console.
How do I create a file with sudo using tee?
Use `echo 'content' | sudo tee /etc/file` to write content to a system file that requires root permissions.
How do I use tee with a heredoc?
Use `cat << 'EOF' | tee file.txt` followed by content and `EOF` to write multi-line content to a file.
Related File Management
fileDetermines and displays the file type (text, binary, image, etc.).
diffCompares two files line by line and shows the differences.
statDisplays detailed file and filesystem information (size, permissions, timestamps).
mkdirCreates a new directory (folder). Use -p to create nested directories.
lnCreates links between files. Use -s for symbolic (soft) links.
touchCreates an empty file or updates the timestamp of an existing file.