# Cheatsheet & Examples: tee

## Basic Usage: Output to a File  
Example Usage:  
`tee filename`  

What it does:  
Writes the standard output to the specified file. If the file doesn’t exist, it creates it. If it does, it appends to it.  

Command-line Arguments Explained:  
- `filename`: The name of the file to write to. It must be specified.  

## Redirecting Output While Running a Command  
Example Usage:  
`command | tee filename`  

What it does:  
Redirects the output of the command to the specified file. The command continues to run, and its output is written to the file.  

Command-line Arguments Explained:  
- `filename`: The name of the file to write to. This is the same as in the first example.  

## Redirecting Both stdout and stderr  
Example Usage:  
`command 2>&1 | tee filename`  

What it does:  
Redirects both standard error and standard output of the command to the specified file. The command continues to run, and its output is written to the file.  

Command-line Arguments Explained:  
- `filename`: The name of the file to write to. This is the same as in the first example.  

## Appending to a File  
Example Usage:  
`tee -a filename`  

What it does:  
Appends the standard output to the specified file. If the file doesn’t exist, it creates it. If it does, it adds the output to the end of the file.  

Command-line Arguments Explained:  
- `-a`: Appends to the file instead of overwriting it. This option is used to avoid overwriting existing content.

## Using with Other Commands  
Example Usage:  
`cat | tee filename`  

What it does:  
Redirects the output of `cat` to the specified file. The `cat` command outputs the file's contents to stdout, which is then written to the file.  

Command-line Arguments Explained:  
- `filename`: The name of the file to write to. This is the same as in the first example.  

## Other Options  
Example Usage:  
`tee -n filename`  

What it does:  
Prevents `tee` from overwriting the file if it already exists. This is useful when you want to append to the file without overwriting.  

Command-line Arguments Explained:  
- `-n`: Prevents `tee` from overwriting the file if it already exists.  

## Using with Multiple Outputs  
Example Usage:  
`tee -a log.txt | grep "error" | tee error.log`  

What it does:  
Redirects output from `tee` to `log.txt` and then filters it with `grep` before writing to `error.log`.  

Command-line Arguments Explained:  
- `log.txt`: The file to write to initially.  
- `error.log`: The file to write to after filtering.  
- `-a`: Appends to `log.txt` instead of overwriting it.  

## Using with `&>`  
Example Usage:  
`command &> tee filename`  

What it does:  
Redirects both standard output and standard error to the specified file.  

Command-line Arguments Explained:  
- `filename`: The name of the file to write to. This is the same as in the first example.
