Startseite > Textverarbeitung & Suche > tee

tee -a: Standard Output to File Appending

The `tee` command is a utility that reads standard input and writes it to both standard output and one or more files simultaneously. The `-a` (append) option, in particular, is used to add new content to the end of a file without overwriting its existing content. This is useful in various scenarios such as log file management and recording script execution results.

Overview

`tee` intercepts data in the middle of a pipeline, displaying it on the screen while simultaneously saving it to a file. The `-a` option changes this saving method to 'append' mode, allowing new data to be added while preserving the existing file content. This is similar to `>>` redirection, unlike `>` which overwrites the file, but `tee` differs in that it continues to pass data to standard output.

Key Features

  • Processes standard input and standard output simultaneously
  • Appends content to files (-a option)
  • Controls data flow in the middle of a pipeline
  • Writes to multiple files concurrently

Main Options

The `tee` command offers various options to control how data is written to files. The `-a` option is particularly essential for appending data while preserving existing file content.

Writing Modes

Erzeugter Befehl:

Kombinieren Sie die Befehle.

Beschreibung:

`tee` Führen Sie den Befehl aus.

Kombinieren Sie diese Optionen und führen Sie die Befehle virtuell zusammen mit der KI aus.

Usage Examples

Learn how to append data to files in various scenarios using the `tee -a` command.

Appending to an Existing File

echo "New log message" | tee -a my_log.txt

Appends the output of the `echo` command to `my_log.txt` while simultaneously displaying it on the screen.

Appending to System Log Files (using sudo)

echo "[$(date)] System event occurred" | sudo tee -a /var/log/syslog

Use `sudo` with `tee -a` to append content to system log files that require elevated privileges. Direct redirection like `>>` might fail because the shell checks permissions before processing the redirection.

Appending to Multiple Files Simultaneously

ls -l | tee -a file1.txt file2.txt

You can append a single output to multiple files at once.

Recording Script Execution Results

./my_script.sh | tee -a script_output.log

Records all output from a script or command to a file while viewing it on the screen in real-time.

Tips & Precautions

Tips and points to consider for more effective use of the `tee -a` command.

Using with sudo

`tee` is very useful when writing to files that require elevated privileges using `sudo`. Direct redirection like `echo "content" >> /path/to/protected_file` can fail because the shell checks permissions before processing the redirection. `tee` executes with `sudo`'s privileges via the pipe, thus resolving this issue.

  • Correct Usage: echo "content" | sudo tee -a /path/to/protected_file
  • Incorrect Usage (Permission Issue): sudo echo "content" >> /path/to/protected_file

Error Handling

`tee` outputs error messages to standard error if a file writing error occurs, but it attempts to continue passing data to the next command in the pipeline. For critical data, consider setting shell options like `set -o pipefail` to ensure the entire pipeline fails if any command within it fails.

File Creation

`tee -a` automatically creates the specified file if it does not exist. Therefore, there is no need to create the file beforehand.


Gleiche Kategorie Befehle