tee Linux Command Guide
The Linux tee command is used to read from standard input and write to both standard output and files simultaneously. It allows users to see data on the screen while saving a copy of it to a file. This command is particularly useful when you want to monitor the progress of a command that produces a lot of output or when you want to save the output for later analysis. By default, tee overwrites existing files, but you can use the -a option to append to the existing files instead.
tee Syntax:
Options:
Option | Description |
---|---|
-a | Append to the given files |
-i | Ignore interrupt signals |
Parameters:
Parameter | Description |
---|---|
file(s) | File(s) to output to |
tee Command Samples:
Redirect Output to a File
Redirects the output of the “ls /etc” command to both the standard output and a file named “output.txt”.
Append Output to a File
Appends the output of the “ls /var/log” command to both the standard output and a file named “logs.txt”.
Read from a File and Display Output
Reads the contents of “input.txt”, displays them on the standard output, and saves them into “output.txt”.
Combine tee with sudo
Uses “sudo” to list the contents of “/root”, displays the output on the standard output, and saves it into “output.txt”.
Ignoring Standard Output and Only Saving to a File
Lists the contents of “/home”, appends the output to “output.txt”, and ignores the standard output using “/dev/null”.
Split Output to Multiple Files
Lists all processes using “ps aux”, saves the complete output to “processes.txt”, and saves only those lines containing “root” to “root_processes.txt”.
Redirect Standard Error to a File
Attempts to list the contents of a nonexistent directory, combines standard output and error, displays them both, and saves the combined output in “error_output.txt”.
tee FAQ:
How do I use tee in Linux?
To use the tee command in Linux, execute the following command:
What is the purpose of tee in Linux?
The tee command in Linux is used to read from standard input and write to standard output and files at the same time.
How can I append output to an existing file using tee in Linux?
To append output to an existing file with tee in Linux, use the -a flag. Here is an example:
How can I display output in the terminal and save it to a file simultaneously using tee in Linux?
To display output in the terminal and save it to a file at the same time with tee in Linux, use it without any flags. For example:
Is there a way to ignore errors and continue with tee in Linux?
To ignore errors and continue with tee in Linux, you can redirect stderr to stdout. Here is an example command:
Can tee be used with sudo in Linux?
Yes, tee can be used with sudo in Linux to save output to a file that requires elevated privileges. Here is an example:
Applications of the tee command
- Capture and display output in the terminal.
- Save output to a file while also displaying it in the terminal.
- Combine the standard output stream with a file.