Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

tee

Copy the input to the screen (or the next command) and to one or more files at the same time.

Niveau
Dagelijks Linux
Groep
Tekst en zoeken
Root nodig
Nee
Pakket
coreutils

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

tee sits in a pipe like a T-piece in a water pipe: data goes through, and a copy goes into files. You see the output and keep it.

A very common use: write to a file that only root may change, from a normal user, with echo ... | sudo tee FILE. The redirect > would run as your user and fail; tee runs with sudo.

Zo schrijf je het

tee [OPTION]... [FILE]...

Opties 6

Meest gebruikt

-a, --append
Add to the end of the files instead of overwriting them.
-i, --ignore-interrupts
Ignore Ctrl+C (the interrupt signal).
-p
Report errors when writing to pipes (and go on with other outputs).
--output-error[=MODE]
What to do when a write fails: warn, warn-nopipe, exit, exit-nopipe.
--help
Show help.
--version
Show the version.

Voorbeelden 11

$ als gewone gebruiker# als root (of met sudo)

  1. 1See the output and save it

    dnf check-update | tee updates.txt

    You watch the list and keep a copy for your change ticket.

  2. 2Write a root-owned file from a normal user

    echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/90-forward.conf

    sudo echo ... > file fails because the redirect is not done by sudo.

  3. 3Add a line to a root-owned file

    echo "192.168.122.20 server2" | sudo tee -a /etc/hosts

    -a appends. Without it the file would be replaced.

  4. 4Hide tee's copy on the screen

    echo "export EDITOR=vim" | sudo tee /etc/profile.d/editor.sh > /dev/null

    Only the file gets the text.

  5. 5Write several files at once

    ip -br a | tee /tmp/net-$(hostname).txt /srv/reports/net-latest.txt

    One command, two copies.

  6. 6Log a long script and still watch it

    ./upgrade.sh 2>&1 | tee -a /var/tmp/upgrade-$(date +%F).log

    2>&1 sends errors into the pipe too, so the log is complete.

  7. 7Save the middle of a pipe

    journalctl -u sshd --since today | tee ssh-today.log | grep -c "Failed password"

    The full log is saved; the screen shows only the count of failed logins.

  8. 8Write a heredoc to a protected file

    sudo tee /etc/yum.repos.d/local.repo > /dev/null <<'EOF'
    [local-baseos]
    name=Local BaseOS
    baseurl=file:///repo/BaseOS
    gpgcheck=0
    EOF

    A clean way to create config files in scripts that run as a normal user with sudo.

  9. 9Keep running when you press Ctrl+C

    ping server2 | tee -i ping.log

    With -i, tee is not stopped by Ctrl+C, so the log gets the last lines.

  10. 10Record a kubectl apply for the change log

    kubectl apply -f deploy.yaml 2>&1 | tee -a changes.log

    Every change is visible now and stored for later.

  11. 11Count and save matches at the same time

    grep "ERROR" app.log | tee errors.txt | wc -l

    The number on the screen, the lines in the file.

Let op

  • Without -a, tee overwrites the file.
  • In sudo tee, only tee runs as root; the command before the pipe runs as you.

Tips om te onthouden

  • tee = T-piece: one input, two outputs (screen + file).
  • | sudo tee FILE = write as root. | sudo tee -a FILE = append as root.

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man tee