EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Linux tee Command

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Linux Tutorial » Linux tee Command

Linux tee Command

Introduction to Linux tee Command

The Linux Tee Command is using the standard streams in which it will read the standard input and writes it to both standard output and one or more files, effectively duplicating its input. Basically the tee command is useful to display the standard output of the give command or program and write the same output in the output file.

The tee command or the utility was written by Mike Parker, Richard M. Stallman, and David MacKenzie.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

tee [ -a ] [ -i ] [ File Name ... ]

  • -a: –append ( Do not overwrite the files. Instead append to the given files )
  • -i: –ignore-interrupts – Ignore interrupt signals.
  • File Name: A file or the list of files. Each of which will get the output file.

How does Linux tee Command Work?

The Linux tee command is normally used to split the output of the input data or the program. It can be both displays and saved the output data in a file. The tee command can be used to collect the intermediate output before the data is changed by another command or program.

The tee Commands will Return the Below Exit Values:

  • 0: After executing the Linux tee command, the exit value of the command is “0”. Then the command is expected correctly.
  • Greater than 0: After executing the Linux tee command, the exit value of the command is greater than “0”. Then the command is not expected correctly.

The tee command reads the standard input and then writes its content to standard output. It will simultaneously copy the data into the directed or specified file or variables. The tee command syntax may differ depending on the requirement.

Examples to Implement Linux tee Command

This is the most basic usage of the tee command to display the output of the command and parallelly will write in the output file.

Popular Course in this category
Linux Training Program (16 Courses, 3+ Projects)16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,013 ratings)
Course Price

View Course

Related Courses
Kali Linux Training (3 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

Example #1 – tee Command to Write Multiple File

Command:

ls -ltrh
ls -ltrh | tee tee_output.txt
cat tee_output.txt

Explanation:

In the above tee command, we are getting the details information of the “/” directory with the help of “ls –ltrh” command (refer screenshot 1 (a)). The same output will provide to tee command as input (with the help of pipe). Then tee command will do both the operation of displaying the output of the command as well as write the output in output file “tee_output.txt” (refer screenshot 1 (b) & (c)).

Output:

Linux tee Command Example 1

Screenshot 1 (a)

Linux tee Command Example 1

Screenshot 1 (b)

Linux tee Command Example 1

Screenshot 1 (c)

Example #2 – tee Append Command

In tee command, we are having the functionality to write the multiple output files at the same time. Just we need to pass the different files separated by space.

Command:

date
date | tee file1.out file2.out file3.out
cat f1.out
cat f2.out
cat f3.out

Explanation:

As per the above command, we are using the date command to get the current system timestamp (refer screenshot 2 (a)). Now we need to display or print the output of the command as well as the same output will write in different multiples files. It will helpful for multimodal text process applications to write the output of the command in multiple files. We can perform the same operation with the help of the tee command (refer screenshot 2 (b)).

Output:

Linux tee Command Example 2

Screenshot 2 (a)

Linux tee Command Example 2

Screenshot 2 (b)

Example #3 – tee Command to Hide Output

In tee command, we can overwrite the specific output files. We can append the command output to a specific file or output file. We can use the “-a” option to append the command output to specific output files.

Command:

echo "Hello EDUCBA"
cat f1.out
echo "Hello EDUCBA" | tee -a f1.out
cat f1.out

Explanation:

In the above tee command, we are using the echo command to display the written text (refer screenshot 3 (a)). The file “f1.out” having some data in it. Now with the help of tee command, we will write the echo command output to “f1.out” file with appending the command output data with existing file data (refer screenshot 3 (b)).

Output:

echo Example 3

Screenshot 3 (a)

echo Example 3

Screenshot 3 (b)

Example #4 – tee Command with SUDO

In tee command, we can hide the output of the command via redirecting the command output to “null”.

Command:

lsblk | tee f3.out > /dev/null
cat f3.out

Explanation:

In the above tee command, we are using “lsblk” command to get the information of the storage drive to our machine. But in general, if we are using the tee command we are displaying the output of the command. But in some cases, we do not want to display the output just need to redirect it. In the above tee command, we are using “lsblk” command but it is not displaying the output of it (refer the output screenshot.)

Output:

 lsblk Example 4

Example #5

In the tee, we can evaluate the sudo permissions while working with tee commands. While using the redirection operators it will identify or understand the sudo permission available in the command.

Command:

echo "Hello EduCBA" > sudo_file.txt
echo "Hello EduCBA" | sudo tee -a sudo_file.txt
cat sudo_file.txt

Explanation:

In tee directory, we are having the 4 different files in it (refer screenshot 4 (a)). Let consider “sudo_file.txt”. The permission or access is the only reserve for root user only. The “educba” user having sudo access but while writing the “sudo_file.txt” file. It is getting the permission denied error (refer screenshot 4 (b)). Because the redirection of the output command is not supporting by sudo.

To overcome this condition, we can use the sudo in tee command. The tee command having the capability to understand or evaluate the sudo permissions and write to the output file (refer screenshot 4 (c)).

Output:

Sudo Permissions Example 5

Screenshot 4 (a)

Linux tee Command Example 10

Screenshot 4 (b)

Linux tee Command Example 5

Screenshot 4 (c)

Conclusion

We have seen the uncut concept of “Linux Tee Command” with the proper example, explanation, and command with different outputs. The tee command is a command-line interpreter. It is using the concept of the standard stream to reads standard input and writes the standard output to single or multiple files parallelly.  For evaluating the number of command and command output results, the tee command is useful.

Recommended Articles

This is a guide to Linux tee Command. Here we discuss the Introduction to Linux tee Command and how it works along with its Examples and Code Implementation.  You can also go through our other suggested articles to learn more –

  1. Linux System Commands
  2. Linux Filter Commands
  3. Kali Linux Commands
  4. Top Commands in Linux

Linux Training Program (16 Courses, 3+ Projects)

16 Online Courses

3 Hands-on Projects

160+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
Linux Tutorial
  • Linux Commands
    • CAT Command in Linux
    • PS command in Linux
    • Mkdir Command in Linux
    • rmdir command in Linux
    • mv command in Linux
    • Linux rm Command
    • CHMOD Command in Linux
    • Zip Command in Linux
    • Linux Route Command
    • SCP Command in Linux
    • du Command in Linux
    • Less Command in Linux
    • Copy Command in Linux
    • Head Command in Linux
    • Help Command in Linux
    • Linux History Command
    • hostname Command in Linux
    • GREP Command in Linux
    • Linux Curl Command
    • Linux List Users
    • Linux List Directories
    • Linux Directory Permissions
    • Find Directory Linux
    • Linux User Permission
    • Linux Add User
    • Linux Delete User
    • Linux uniq
    • Linux Filter Commands
    • Linux sort Command
    • Linux Sort by Size
    • Remove dir Linux
    • Linux tr Command
    • Linux Touch Command
    • Linux More Command
    • Linux Cal Command
    • Linux OD
    • Linux Zcat
    • Linux tracepath
    • Linux Sleep
    • Linux bg Command
    • Linux WC
    • Linux Read
    • Linux Write
    • Linux Rename Command
    • Linux Free Command
    • Linux tac
    • Linux Shred
    • Linux wget
    • Linux Date
    • Linux Untar
    • Linux JQ
    • Linux Xargs
    • Linux Mount Command
    • Linux mdadm
    • Linux comm
    • Linux tee Command
    • Tail Command in Linux
    • Linux gzip
    • Linux Directory Structure
    • Linux Alias Command
    • Linux Inode
    • Linux Ping
    • Linux Watch Command
    • Linux Split Command
    • Linux Size
    • Linux Network Stack
    • Linux Diff Command
    • Vim Command in Linux
    • Linux nslookup
  • Basic
    • Introduction to Linux
    • What is Linux
    • Careers in Linux
    • How to Install Linux
    • How to Install Ubuntu
    • How to Install Ubuntu Server
    • How To Install Debian
    • Advantage Of Linux
    • What is Raid in Linux
    • What is Raid 5?
    • Raid 5 vs Raid 6
    • Careers in Linux Administration
    • What is Shell in Linux?
    • Bash Shell in Linux
    • Linux Emulator
    • Linux Operators
    • Linux Apps
    • Linux Keyboard Shortcuts
    • Linux Crontab
    • What is Raid Storage
    • What is Vmware
    • What is RedHat
    • What is Debian
    • Uses of Ubuntu
    • Career In RedHat
    • Linux Distributions
    • Linux From Scratch
    • Linux Format
    • Cheat Sheet Linux
  • Advanced
    • Install VMware
    • Install GRUB
    • Chown Command in Linux
    • Linux Network Command
    • Configuring Linux Network Environment
    • DNS Configuration in Linux
    • Linux Users
    • Create User in Linux
    • DHCP Server in Linux
    • FTP Server in Linux
    • Linux Regular Expression
    • IP Command in Linux
    • Set IP Address in Linux
    • Tar Command in Linux
    • Dynamic Host Configuration Protocol
    • Linux Kill Signals
    • Linux File Permissions
    • Linux File System
    • Linux Create File
    • Linux Concatenate Files
    • Linux Join
    • Linux Process Management
    • Linux Proxy Server
    • Linux System Commands
    • Linux System Logging
    • NFS in Linux
    • Linux LVM
    • Linux Repository
    • Samba Server in Linux
    • Top Commands in Linux
    • Types of Shells in Linux
    • Shell Script Types For Linux
    • GCC Command in Linux
    • Monolithic Kernel
    • YARN Package Manager
    • Linux Sed Replace
    • Linux List Groups
    • Linux Container
    • Linux Find File by Name
    • Create Folder in Linux
    • Linux Move Folder
    • Linux Error Codes
    • Linux Automount
    • Linux HugePages
  • Kali Linux
    • Install Kali Linux
    • Kali Linux Uses
    • Kali Linux Commands
    • Kali Linux Repository
    • Kali Linux Packages
    • Kali Linux root password
    • Kali Linux Alternative
  • Interview Questions
    • Linux Interview Questions
    • Linux System Administration Interview
    • Red Hat Interview Questions

Related Courses

Kali Linux Training

Linux Training Course

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Linux Training Program (16 Courses, 3+ Projects) Learn More