tee: Split Output
tee is named after the T-shaped pipe fitting: it reads its input and writes it both to the screen (standard output) and to one or more files. You use it to save a copy of a pipeline's output while still watching it, to log long-running commands and to write files that need root privileges.
Basic use
echo "deploy started" | tee deploy.log
cat deploy.logdeploy started
deploy started
The text appeared on the screen and ended up in deploy.log.
Appending with -a
Like >, plain tee overwrites the file. -a appends, like >>:
echo "step 1: build" | tee build.log
echo "step 2: test" | tee -a build.log
echo "step 3: package" | tee -a build.log > /dev/null
echo "--- build.log:"
cat build.logstep 1: build
step 2: test
--- build.log:
step 1: build
step 2: test
step 3: package
> /dev/null throws away tee's screen copy when you only want the file write; the third step was logged silently.
Saving the middle of a pipeline
Because tee passes its input through, you can put it between two commands and capture an intermediate result:
cat > orders.txt <<'TXT'
ada 3
linus 1
grace 5
ada 2
TXT
sort orders.txt | tee sorted.txt | awk '{ s[$1] += $2 } END { for (n in s) print n, s[n] }' | sort
echo "--- intermediate sorted.txt:"
cat sorted.txtada 5
grace 5
linus 1
--- intermediate sorted.txt:
ada 2
ada 3
grace 5
linus 1
Several files at once
printf 'line one\nline two\n' | tee copy1.txt copy2.txt | wc -l
cat copy1.txt copy2.txt | wc -l2
4
Logging a script's output
A common pattern is to send everything a command prints, normal output and errors, through tee into a log file. 2>&1 merges errors into the output first:
run_backup() {
echo "copying files..."
ls missing-folder
echo "backup finished"
}
run_backup 2>&1 | tee backup.log
echo "--- lines in log: $(wc -l < backup.log)"
grep -c 'cannot access' backup.logcopying files...
ls: cannot access 'missing-folder': No such file or directory
backup finished
--- lines in log: 3
1
Note: In a pipeline,
$?is the exit status of the last command, which istee(almost always 0). If you need the status of the command beforetee, use${PIPESTATUS[0]}orset -o pipefail(see the error-handling lesson).
ls missing-folder 2>/dev/null | tee out.txt
echo "\$? = $?, PIPESTATUS = ${PIPESTATUS[*]}"$? = 0, PIPESTATUS = 2 0
Writing root-owned files: sudo tee
A classic puzzle: sudo echo "text" > /etc/file fails with "Permission denied". Why? The redirection > is done by your shell before sudo even starts, and your shell isn't root. tee solves it because it opens the file itself, and it runs under sudo:
echo "127.0.0.1 shop.local" | sudo tee -a /etc/hosts
echo "vm.swappiness=10" | sudo tee /etc/sysctl.d/99-swap.conf > /dev/nullYou can see the same rule as a normal user: a redirect into a directory you can't write fails, no matter what command it's attached to:
echo "test" > /etc/motd-test
echo "status: $?"/w/main.sh: line 1: /etc/motd-test: Permission denied
status: 1
Test yourself
What does cmd | tee out.txt do?
- Saves the output in out.txt only
- Shows the output and saves it in out.txt
- Appends out.txt to the output
- Runs cmd twice
Which option makes tee append instead of overwrite?
- -o
- -a
- -i
- >>
Why does sudo echo hi > /etc/x fail while echo hi | sudo tee /etc/x works?
- echo can't run as root
- The
>redirection is performed by your non-root shell; tee opens the file itself as root - tee is faster
- sudo doesn't support echo