More

    Pipeline Redirection using the tee command in Linux

    The tee command reads from standard input and write to standard output and files at the same time. It can copy standard input to each FILE, and to standard output. The tee command is useful when you want not only to send some data down a pipe, but also to save a copy. This tutorial is all about Pipeline Redirection using the tee command in Linux.

    Analogy:  imagine data as water flowing through a pipeline, tee can be visualized as a “T” joint in the pipe which directs output in two directions.

    tee command

    Pipeline Examples of using the tee command

    1. Pipeline Redirection using the tee command in Linux – Redirect the output of the ls command to file and pass it to less to be displayed on terminal

      ls -l | tee /home/technnix/saved-output | less
    2.  Count number of characters in our file and also save the output to new text file

      # wc -l index.html| tee html_wc.txt
      308 index.html
      
      # cat html_wc.txt
      308 index.html
      
      

       

    3.  List the last 10 changed files in the Linux file system and dave the output

      # ls -t | head -n 10 | tee /tmp/ten-last-changed-files.txt
      tmp
      root
      boot
      run
      etc
      dev
      sys
      proc
      srv
      var
      
      # cat /tmp/ten-last-changed-files.txt
      tmp
      root
      boot
      run
      etc
      dev
      sys
      proc
      srv
      var

       

    4. Pipeline Redirection using the tee command in Linux – Check Disk usage using df -h and save the output to a file

      # df -h | tee disk_usage_file.txt
      Filesystem      Size  Used Avail Use% Mounted on
      devtmpfs        4.0M     0  4.0M   0% /dev
      tmpfs           865M     0  865M   0% /dev/shm
      tmpfs           346M  8.0M  339M   3% /run
      /dev/sda1        38G  2.8G   34G   8% /
      /dev/sda14       64M  7.0M   57M  11% /boot/efi
      tmpfs           173M     0  173M   0% /run/user/1000
      
      # cat disk_usage_file.txt
      Filesystem      Size  Used Avail Use% Mounted on
      devtmpfs        4.0M     0  4.0M   0% /dev
      tmpfs           865M     0  865M   0% /dev/shm
      tmpfs           346M  8.0M  339M   3% /run
      /dev/sda1        38G  2.8G   34G   8% /
      /dev/sda14       64M  7.0M   57M  11% /boot/efi
      tmpfs           173M     0  173M   0% /run/user/1000

       

    Conclusion

    The tee command is useful when you happen to be transferring a large amount of data and also want to summarize that data without reading it a second time e.g when downloading a huge file, we often want to verify its signature or checksum immediately.

    # wget -O - https://example.com/dvd.iso | tee ubuntu2204.iso | sha1sum > ubuntu2204.sha1
    
    

    The command above is efficient since it interleaves the download and SHA1 computation. We’ll get the checksum for free, because the entire process parallelizes so well.

     

    See also:

    1. How copy all the content of a file in Vim to clipboard

     

    Recent Articles

    Related Articles

    Leave A Reply

    Please enter your comment!
    Please enter your name here