This suits me to a tee

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,237
Reaction score
2,989
Credits
28,493
The tee command is tool that reads from standard input and writes to both standard output and one or more files simultaneously.

I find this handy when I'm outputting a file and the stdout at the same time.

Consider the following command
Code:
df -h | tee disk_usage.txt

This outputs the results of my df command to a text file. I could do this with the ">" redirect as well.
However, what if I wanted to output to 2 or 3 files at the same time?

Code:
ls -l | tee file1.txt file2.txt

tee will let me do that.

Code:
echo "New line" | tee -a file.txt

Again I could do this with the ">" redirect. But the ">" redirect has some limitations.
For example, consider the following

Code:
echo "dos2unix is my favorite command." > favorite.txt

cat favorite.txt

We will see the line we echo'd out to the file. Now let's run it again.

Code:
echo "tee is my second favorite command." > favorite.txt

cat favorite.txt

Oops, what happened, we lost our first line of text, it was overwritten by the second echo command.
So we can get around that by using a double re-direct.

Code:
echo "but dos2unix is still my favorite command." >> favorite.txt

cat favorite.txt

Notice now it didn't erase the first line, it appended a second line.
That's what the -a flag does when you are using tee.

Another nice option when using tee, is that I can ignore system interrupt errors while watching the output.

Code:
some_command | tee -i output.txt


Like other commands, depending on what you are doing, you may have to use it with sudo.

Code:
echo "New line" | sudo tee -a /etc/some_file.conf

Hopefully this will be helpful to someone.
 


I use tee all the time. I run netdiscover on two of the system consoles and use tee so I can stream the data in an xterm. I use netdiscover -i eth0 -p | tee filenamehere and then tail -f filenamehere to stream the data elsewhere. This lets me keep an eye on ARP requests on my network. This tells me when a new device connects to the network because the device will seek the MAC address for the gateway. A new device will generally always send an ARP request when connecting to a network which then shows up on my screen.

Signed,

Matthew Campbell
 
Thanks @dos2unix for the heads up on tee.

Just on the difference between tee and the redirects > and >>, tee will allow the output of the command to be shown on screen at the same time as the command's output is being saved to the file designated by tee after the pipe, which is not the case when just using the redirects > and >>. Below is an example of this particular capability of tee.

Having a box of usb sticks and wishing to check some info on them, and having just a single usb port handy to do it, the user can use the dmesg command to output the info dmesg provides on screen about the usbs and also record the info for later reference in a single file. In this case the particular interest was in determining the manufacturer and the capacity of a bunch of usbs.

Plugging the usbs in and out of the port, one after another, and running the command below for each usb, the info appears on screen and is also accumulated in a file designated by the tee command using the -a option to append the output and not overwrite it:

Code:
$ dmesg | tail -n 20 | tee -a dmesgOutput
[    6.746503] 8021q: 802.1Q VLAN Support v1.8
[   33.910545] systemd-journald[471]: Time jumped backwards, rotating.
[ 4558.242641] usb 1-2.2: new high-speed USB device number 7 using xhci_hcd
[ 4558.354200] usb 1-2.2: New USB device found, idVendor=0718, idProduct=067d, bcdDevice= 1.00
[ 4558.354219] usb 1-2.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 4558.354225] usb 1-2.2: Product: ImationFlashDriv
[ 4558.354230] usb 1-2.2: Manufacturer: Imation  <-----------------------------------MAKE
[ 4558.354234] usb 1-2.2: SerialNumber: 07B31A0351083061
[ 4558.356304] usb-storage 1-2.2:1.0: USB Mass Storage device detected
[ 4558.356818] scsi host9: usb-storage 1-2.2:1.0
[ 4558.389436] usbcore: registered new interface driver uas
[ 4559.456280] scsi 9:0:0:0: Direct-Access     Imation  ImationFlashDriv PMAP PQ: 0 ANSI: 0 CCS
[ 4559.457011] scsi 9:0:0:0: Attached scsi generic sg1 type 0
[ 4560.025568] sd 9:0:0:0: [sda] 7826688 512-byte logical blocks: (4.01 GB/3.73 GiB) <--SIZE
[ 4560.026101] sd 9:0:0:0: [sda] Write Protect is off
[ 4560.026107] sd 9:0:0:0: [sda] Mode Sense: 23 00 00 00
[ 4560.026291] sd 9:0:0:0: [sda] No Caching mode page found
[ 4560.026296] sd 9:0:0:0: [sda] Assuming drive cache: write through
[ 4560.055681]  sda: sda1
[ 4560.055873] sd 9:0:0:0: [sda] Attached SCSI removable disk

As the info appeared on screen, the user could group the usb into piles as the info was forthcoming, but still had the info on file for future reference.
 
Last edited:
echo "New line" | tee -a file.txt
It's worth adding if you need elevated permissions to write to file then sudo needs to be added prior tee instead of prior echo, albeit this is beginners mistake easy to overlook:

OK:
Bash:
echo "New line" | sudo tee -a file.txt

Will not work:
Bash:
sudo echo "New line" | tee -a file.txt
 

Members online


Latest posts

Top