Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»tee Command in Linux with Examples

    tee Command in Linux with Examples

    By RahulJuly 1, 20224 Mins Read

    tee is a command in Linux that reads from standard input and writes the output to both standard output and one or more files, effectively duplicating the input. It is typically used in shell scripts to tee command output to both a file and the console.

    Advertisement

    The Tee command in Linux is mostly used in combination with other commands, it reads the input and in response to that writes the output to one or more files. It does this so that the output can be displayed as well as saved to a file at the same time. In this article, we will learn more about the tee command, from its syntax to its use along with examples to help you understand it better.

    Linux tee command with examples

    Syntax

    The syntax of the tee command is provided below.

    tee [OPTIONS]... [FILE]...
    

    Here in this OPTIONS could be the following:

    • -a (–append): used to append to add to file and not overwrite it.
    • -i : in order to ignore the interrupt signals.

    For [FILE] include the filename which can be one or more than one file.

    Tee Command in Linux

    The basic way in which the tee command is commonly used is to write to a file and display the output i.e stdout (standard output).

    Users can write to a single as well as multiple files, hide output, they can append to a file without overwriting it, and also useful to ignore any interrupts while executing the file. Let’s understand these ones by one with examples.

    1. Write to a File
    2. We can use the tee command to write to a file and to demonstrate this we will use the command “df” to check the file system’s disk space and write all the information that we get as output in a file named “exFile.txt”.

      df -h | tee exFile.txt 
      

      Now go and check the file “exFile.txt” using the cat command.

      cat exFile.txt  
      

    3. Write To Multiple Files
    4. In order to write to multiple files, we’ll use the same above example but save the content into two different files. All you’ve to do is separate the file names by space.

      df -h | tee firstFile.out secondFile.out
      

      Now again if you check both the files using the cat command the content will be displayed.

      cat firstFile.out  
      
      cat secondFile.out  
      

      In this way, we can write to one or multiple files and save our output for later use.

    5. Append to a File
    6. This option “-a” is extremely useful as it helps in appending to a file without overwriting it. In this way, we can save the previous information and later add new information to the file. For this purpose first, we will create a file “helloFile.txt” and inside write “Hello”.

      echo "Hello"  | tee -a helloFile.txt  
      

      Now we’ll spend the word “World” in it.

      echo "World"  | tee -a helloFile.txt  
      

      Now, if we view the file using the cat command we will see that it appended the last word instead of overwriting it over the previous.

      cat helloFile.txt  
      

      In this way, we can append it into a file without overwriting it.

    7. Hide the Output
    8. In case you don’t want the output to be displayed on the terminal and simply save it in the file using the command “>/dev/null” along with the tee command.

      sudo echo "There?" | tee -a helloFile.txt >/dev/null  
      

      Later we checked the output using the cat command.

    9. Ignore any Interrupts
    10. Sometimes we want to execute a text file or any other format of the file but some interrupts occur and the process stops. To ignore these interrupts and want the tee to exit smoothly we use “-i” along with the tee command.

      In the example, we will ping google and use the interrupt command and while it’s executing we will interrupt it with CTRL+C.

      ping google.com | tee -i newFile.txt  
      

      Here you can see that while the command was running it was interrupted but still it executed smoothly. We can verify the content was added to the file by viewing the file using the cat command:

      cat newFile.txt  
      

    11. Using tee with Sudo
    12. In case you want to write to a file that belongs to root or a sudo user you have to use sudo along with the command.

      sudo echo "Please?" | sudo tee -a helloFile.txt  
      

      This way you can have access to the file easily.

    Conclusion

    Sometimes the user wants to write the output they get in the terminal to a file and for this purpose, the tee command is used. In this article we told you various ways how the tee command is used in Linux, we can write to multiple files as well as hide output or append to a file without overwriting it. Examples are provided in the case to help you understand the usage better of each command.

    command shell tee
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    tail Command in Linux with Examples

    Uptime Command in Linux

    A Quick Reference Guide to Uptime Command in Linux

    fallocate Command in Linux (Allocate the Space for a File)

    fallocate Command in Linux (Allocate the Space for a File)

    View 1 Comment

    1 Comment

    1. Sandeep Yadav on January 19, 2022 7:35 am

      nice blog bro

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • tail Command in Linux with Examples
    • What is a Orphan Process in Unix/Linux
    • How To Display Warning Message to Unauthorized SSH Access
    • How to Set a Custom SSH Login Banner and MOTD
    • Understanding Reverse DNS: What it is and Why it Matters?
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.