top of page
Writer's pictureSiddhesh Kadam

Linux "tee" Command


Linux offers a vast array of command-line utilities, each designed to perform specific tasks efficiently. One such versatile tool is the "tee" command, which is often overlooked but can be a valuable asset to any Linux user or system administrator. In this blog post, we will explore the Linux "tee" command, its functionality, and various use cases.


What is the "tee" Command?


The Linux "tee" command in Linux is a straightforward yet powerful utility used to read from standard input and write to standard output and files simultaneously. Its name comes from the 'T' junction in plumbing, where water flows in two directions.


The basic syntax for the "tee" command is as follows:

tee [OPTION]... [FILE]...


Here's a breakdown of how it works:


OPTION: You can use various options to modify the behaviour of the "tee" command, such as '-a' to append to existing files, or '-i' to ignore interrupt signals.


FILE: You can specify one or more filenames as arguments, and `tee` will write the standard input to these files.


Practical Usage of the "tee" Command


1. Logging Output


One of the most common use cases for "tee" is to log the output of a command or script. For instance, if you want to save the output of a system command to a file and also display it on your terminal, you can use "tee". Here's an example:

[root@siddhesh ~]# ls /tmp/|tee list_of_files.txt
openldap-tlsmc-certs--279C03081A4CB43BE6F7C5ECCD97A532A2BF23AE390AF8441B17AE6E710728BB
systemd-private-4a83fd693525471e8401e0ee7151879b-chronyd.service-oE3oWB
systemd-private-4a83fd693525471e8401e0ee7151879b-mariadb.service-F78wRo
yum_save_tx.2023-10-29.09-44.eIMRx0.yumtx
yum_save_tx.2023-10-29.09-45.SFLJYO.yumtx
yum_save_tx.2023-10-29.09-56.i_z_ei.yumtx
yum_save_tx.2023-10-29.09-58.O_VhQK.yumtx
yum_save_tx.2023-10-29.09-58.TXn2cZ.yumtx
[root@siddhesh ~]# cat list_of_files.txt
openldap-tlsmc-certs--279C03081A4CB43BE6F7C5ECCD97A532A2BF23AE390AF8441B17AE6E710728BB
systemd-private-4a83fd693525471e8401e0ee7151879b-chronyd.service-oE3oWB
systemd-private-4a83fd693525471e8401e0ee7151879b-mariadb.service-F78wRo
yum_save_tx.2023-10-29.09-44.eIMRx0.yumtx
yum_save_tx.2023-10-29.09-45.SFLJYO.yumtx
yum_save_tx.2023-10-29.09-56.i_z_ei.yumtx
yum_save_tx.2023-10-29.09-58.O_VhQK.yumtx
yum_save_tx.2023-10-29.09-58.TXn2cZ.yumtx
[root@siddhesh ~]#

In this example, the 'ls' command lists files and directories in the /tmp/ folder. The 'tee' command takes the standard output of 'ls', writes it to the 'list_of_files.txt' file, and simultaneously displays it on the terminal.


2. Appending to Files


You can use the '-a' option with 'tee' to append data to an existing file, rather than overwriting it. For example:

[root@siddhesh ~]# ls /root/|tee -a list_of_files.txt
config-3.10.0-1160.45.1.el7.x86_64
[root@siddhesh ~]# cat list_of_files.txt
openldap-tlsmc-certs--279C03081A4CB43BE6F7C5ECCD97A532A2BF23AE390AF8441B17AE6E710728BB
systemd-private-4a83fd693525471e8401e0ee7151879b-chronyd.service-oE3oWB
systemd-private-4a83fd693525471e8401e0ee7151879b-mariadb.service-F78wRo
yum_save_tx.2023-10-29.09-44.eIMRx0.yumtx
yum_save_tx.2023-10-29.09-45.SFLJYO.yumtx
yum_save_tx.2023-10-29.09-56.i_z_ei.yumtx
yum_save_tx.2023-10-29.09-58.O_VhQK.yumtx
yum_save_tx.2023-10-29.09-58.TXn2cZ.yumtx
config-3.10.0-1160.45.1.el7.x86_64
[root@siddhesh ~]#

This command appends 'ls' of /root/ directory to the 'list_of_files.txt' without erasing its previous content.


3. Splitting Output


'tee' can be used to split the output of a command and send it to multiple files. For example, you might want to save the standard output and standard error streams separately:

[root@siddhesh ~]# ls -ls /siddhesh 2> error.log || ls  /home/|tee list_of_files.txt
centos
[root@siddhesh ~]# cat list_of_files.txt
centos
[root@siddhesh ~]# cat error.log
ls: cannot access /siddhesh: No such file or directory
[root@siddhesh ~]#

In this case, standard output goes to 'list_of_files.txt', and standard error goes to 'error.log'.


4. Interactive Use


'tee' can also be useful in interactive situations. For instance, you can use it to make a backup of a file while editing it in a text editor like Vim:

[root@siddhesh ~]# vim error.log |tee test.log.bak

This command opens the 'error.log' in Vim. When you save your changes in Vim, 'tee' makes a backup of the file with the extension 'test.log.bak'.


5. Real-Time Monitoring


Another practical application of 'tee' is for real-time monitoring of log files. For example, you can monitor a log file and simultaneously write the content to another file:

[root@siddhesh ~]# tail -f /var/log/dmesg |tee dmesglog.txt
[    7.058257] [drm] vram aper at 0xF0000000
[    7.065486] [drm] size 33554432
[    7.072370] [drm] fb depth is 16
[    7.079061] [drm]    pitch is 2048
[    7.102844] EDAC sbridge: Seeking for: PCI ID 8086:2fa0
[    7.102850] EDAC sbridge:  Ver: 1.1.2
[    7.110322] fbcon: cirrusdrmfb (fb0) is primary device
[    7.233614] Console: switching to colour frame buffer device 128x48
[    7.242725] cirrus 0000:00:02.0: fb0: cirrusdrmfb frame buffer device
[    7.246737] [drm] Initialized cirrus 1.0.0 20110418 for 0000:00:02.0 on minor 0
^C
[root@siddhesh ~]# cat dmesglog.txt
[    7.058257] [drm] vram aper at 0xF0000000
[    7.065486] [drm] size 33554432
[    7.072370] [drm] fb depth is 16
[    7.079061] [drm]    pitch is 2048
[    7.102844] EDAC sbridge: Seeking for: PCI ID 8086:2fa0
[    7.102850] EDAC sbridge:  Ver: 1.1.2
[    7.110322] fbcon: cirrusdrmfb (fb0) is primary device
[    7.233614] Console: switching to colour frame buffer device 128x48
[    7.242725] cirrus 0000:00:02.0: fb0: cirrusdrmfb frame buffer device
[    7.246737] [drm] Initialized cirrus 1.0.0 20110418 for 0000:00:02.0 on minor 0
[root@siddhesh ~]#

This command displays the latest entries from '/var/log/dmesg' in real-time and stores them in 'dmesglog.txt'.

コメント

5つ星のうち0と評価されています。
まだ評価がありません

評価を追加
bottom of page