05-E.8.3: File Output Manipulation - tee & /dev/null
- Page ID
- 32647
The tee Command
The tee command reads the standard input and writes it to both the standard output and one or more files. The command is named after the T-splitter used in plumbing. It basically breaks the output of a program so that it can be both displayed and saved in a file. It does both the tasks simultaneously, copies the result into the specified files or variables and also displays the result.
SYNTAX:
tee [ OPTION ] [FILE]...
tee Command Options :
Options | Option Meaning |
---|---|
-a, --append | append to the given FILEs, do not overwrite |
-i, --ignore-interrupts | ignore interrupt signals |
In each of the following examples, the output is shown on the screen and also placed in the specified file.
# Output of the ls command is sent to the terminal as well as the the file output.txt, as can be seen with the cat of the file.
pbmac@pbmac-server $ ls | tee output.txt
one.txt
states.list
three.txt
two.txt
pbmac@pbmac-server $ cat output.txt
one.txt
states.list
three.txt
two.txt
# Output from the ls -l command is appended to the output.txt file
pbmac@pbmac-server $ ls -l | tee -a output.txt
total 8
-rw-r--r-- 1 pbmac pbmac 0 Aug 5 12:31 one.txt
-rw-r--r-- 1 pbmac pbmac 38 Aug 5 12:32 output.txt
-rw-r--r-- 1 pbmac pbmac 27 Aug 5 11:11 states.list
-rw-r--r-- 1 pbmac pbmac 0 Aug 5 12:31 three.txt
-rw-r--r-- 1 pbmac pbmac 0 Aug 5 12:31 two.txt
pbmac@pbmac-server $ cat output.txt
one.txt
states.list
three.txt
two.txt
total 8
-rw-r--r-- 1 pbmac pbmac 0 Aug 5 12:31 one.txt
-rw-r--r-- 1 pbmac pbmac 38 Aug 5 12:32 output.txt
-rw-r--r-- 1 pbmac pbmac 27 Aug 5 11:11 states.list
-rw-r--r-- 1 pbmac pbmac 0 Aug 5 12:31 three.txt
-rw-r--r-- 1 pbmac pbmac 0 Aug 5 12:31 two.txt
pbmac@pbmac-server $
Linux /dev/null
All data written on a /dev/null or /dev/zero special file is discarded by the system. Use /dev/null to send any unwanted output from program/command.
Syntax:
command >/dev/null
It is possible to redirect a command's standard output messages to /dev/null where it is ignored by the shell.
command 1>/dev/null
This syntax redirects the command error output messages to /dev/null where it is ignored by the shell. OR
command 2> /dev/null OR command &>/dev/null
This syntax redirects both standard output and error output messages to /dev/null where it is ignored by the shell.
Adapted from:
"tee command in Linux with examples" by Anuragrawat1, Geeks for Geeks is licensed under CC BY-SA 4.0
"/dev/null discards unwanted output" by Variety of contributors is licensed under CC BY-NC-SA 3.0