---
title: "Master Unix tee Commands for Real-World Linux Admin Tasks"
description: "Simplify Linux output management and streamline your workflow with the “Unix tee” command. This tutorial guides you through its practical applications."
canonical: "https://adamtheautomator.com/unix-tee/"
---

# Master Unix tee Commands for Real-World Linux Admin Tasks

> Simplify Linux output management and streamline your workflow with the “Unix tee” command. This tutorial guides you through its practical applications.

Source: https://adamtheautomator.com/unix-tee/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Master Unix tee Commands for Real-World Linux Admin Tasks](https://adamtheautomator.com/wp-content/uploads/2022/09/Practical-Linux-Unix-Tee-Commands-for-the-Linux-Admin.jpg)

# Master Unix tee Commands for Real-World Linux Admin Tasks

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)27 March 20246 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Command Line](/tag/command-line/)[Linux](/tag/linux/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Redirecting Commands Outputs (STDOUT/STDERR)](#redirecting-commands-outputs-stdoutstderr)
*   [Saving Commands Outputs to a File](#saving-commands-outputs-to-a-file)
*   [Connecting Programs with Pipes](#connecting-programs-with-pipes)
*   [Combining the Unix Tee (tee) Command, Pipes, and Redirects](#combining-the-unix-tee-tee-command-pipes-and-redirects)
*   [Appending Output to Existing Files](#appending-output-to-existing-files)
*   [Using the tee Command in Conjunction with sudo](#using-the-tee-command-in-conjunction-with-sudo)
*   [Conclusion](#conclusion)

As a Linux admin, there are many occasions when you must save a command’s output to a file, apart from just printing the output on the terminal. Not sure how? Let the Unix tee command do the trick.

In this tutorial, you’ll learn many ways to use the `tee` command to save and append output to a file.

Ready? Read on to take your shell scripting to the next level!

## Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have a Linux system. This tutorial uses [Ubuntu 20.04](https://adamtheautomator.com/install-ubuntu/), but you can use any flavor of Linux.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

## Redirecting Commands Outputs (STDOUT/STDERR)

Before running the `tee` command, it’s crucial to understand how redirects work. In Linux, everything is considered a file, including devices, directories, and even the output of commands. As a result, you can take the output of one command and use that output as input for another command (file redirects).

There are different types of redirects, and each one serves a purpose. But when running the `tee` command, you primarily use two different types of redirects:

*   Standard Output Redirect (STDOUT) (>) – The default output of a command.
*   Standard Error Redirect (STDERR)(2>) – An error message generated by a command and displayed on your screen.

Both redirects take and save outputs of commands to a file, but you’ll see how redirects work below.

Run the `ls` command without arguments to list all contents (files and folders) of your working directory.

```bash
ls
```

Below is the STDOUT of the ls command.

![Getting a STDOUT](https://adamtheautomator.com/wp-content/uploads/2022/09/image-819.png)

Getting a STDOUT  

Next, run the below command to purposely list the contents of a directory (`fake_directory`) that doesn’t exist.

```bash
ls fake_directory
```

You’ll get an error message saying that the directory doesn’t exist. This output is the STDERR of the ls command.

![](https://adamtheautomator.com/wp-content/uploads/2022/09/image-820.png)

Getting STDERR

## Saving Commands Outputs to a File

Suppose you like to save the STDOUT of the `ls` command to a file instead of just printing the output to the terminal. In this case, the redirect operator (`>`) comes in handy.

1\. Run the following command to save/redirect (>) the STDOUT of the ls command to a file named ls\_stdout.txt. Since this command saves the output to a file, you won’t get an output on your terminal.

```bash
ls > ls_stdout.txt
```

2\. Now, run the cat command below to view the contents of your ls\_stdout.txt file.

```bash
cat ls_stdout.txt
```

![](https://adamtheautomator.com/wp-content/uploads/2022/09/image-821.png)

Viewing the contents of the _ls\_stdout.txt_ file

3\. Similarly, run the following command to save the STDERR (2>) of the ls command to a file named ls\_stderr.txt. Again, you won’t get an output on your terminal since the output is saved to a file.

```bash
ls fake_directory 2> ls_stderr.txt
```

4\. Now, run the cat command below to see the contents of the ls\_stderr.txt file.

```bash
cat ls_stderr.txt
```

Below, you can confirm that the STDERR of the ls command was save to the _ls\_stderr.txt_ file.

![Viewing the contents of the ls\_stderr.txt file](https://adamtheautomator.com/wp-content/uploads/2022/09/image-822.png)

Viewing the contents of the _ls\_stderr.txt_ file

5\. Run the below command to save both (&>) STDOUT and STDERR of the ls command to a file named ls\_alloutput.txt.

Why settle for one when you can have both, right? Typically, you might want to store both the outputs in the same file for troubleshooting or logging purposes.

```bash
ls &> ls_alloutput.txt
```

6\. Lastly, run the following cat command to view the ls\_alloutput.txt file’s content

```bash
cat ls_alloutput.txt
```

![Viewing the ls\_alloutput.txt file](https://adamtheautomator.com/wp-content/uploads/2022/09/image-823.png)

Viewing the _ls\_alloutput.txt_ file

## Connecting Programs with Pipes

Apart from just saving outputs to a file, you can accomplish much more using pipes, like connecting programs. In real-world scenarios, you’ll most likely use pipes in conjunction with file redirects and the `tee` command.

A pipe (`|`) is a mechanism for inter-process communication that lets you connect one command’s output to another command’s input. In other words, the pipe takes the STDOUT of one process and connects that STDOUT to the STDIN of another process.

Run the following [grep](https://phoenixnap.com/kb/grep-command-linux-unix-examples) command to find all the lines containing the word tcp in the /etc/services file.

Related:[How to Search via Grep with Regex](https://adamtheautomator.com/grep-with-regex/)

```bash
grep tcp /etc/services
```

![Searching all the lines that contain the word “tcp” from the /etc/services file](https://adamtheautomator.com/wp-content/uploads/2022/09/image-824.png)

Searching all the lines that contain the word “tcp” from the /etc/services file

Now, run the same `grep` command below. But this time, pipe the output of the above `grep` command to the `less` command to display the search results one screen at a time.

```bash
grep tcp /etc/services | less
```

How does the piping work? As you can see below, when you use the pipe character (|), the output of the `grep` command is stored in a temporary location. The output is then passed as an input to the `less` command.

![Piping the output of the above grep command to the less command](https://adamtheautomator.com/wp-content/uploads/2022/09/image-825.png)

Piping the output of the above grep command to the less command

## Combining the Unix Tee (`tee`) Command, Pipes, and Redirects

So far, you’ve learned how the `tee` command, pipes, and redirects work. But why not use all three together for better control of command outputs?

You previously used the piping with redirects, and that’s a huge step. This time, you’ll use piping and the `tee` command to send one command’s output to another for further processing.

Run the below command to send the output of the `ls` command to the screen and the `result.txt` file. The output of `ls` is further piped to the `grep` command to filter only the lines that contain the word `ip`.

```bash
ls | grep 'ip' | tee result.txt
```

![Sending command output to other commands](https://adamtheautomator.com/wp-content/uploads/2022/09/image-826.png)

Sending command output to other commands  

Now, run the `cat` command to verify the contents of the _result_.txt file.

```bash
cat result.txt
```

![Verifying the contents of the result.txt file](https://adamtheautomator.com/wp-content/uploads/2022/09/image-827.png)

Verifying the contents of the _result.txt_ file

## Appending Output to Existing Files

You previously saved the output of commands to a file, but can you update an existing file instead of creating a new one? Yes! Let the `tee` command do the magic.

By default, the `tee` command overwrites the contents of an existing file. But in many cases, you might want to append the output of a command to an existing file. Such as logging all server events in real-time to a log file.

With this feature, the log file gets appended with new entries whenever an event occurs on the server.

Run the below command to take the output of the ls -lR command and append the output (tee -a) to the result.txt file’s content. This command doesn’t provide output since you’re sending the command output to a file.

```bash
ls -lR | tee -a result.txt
```

Now, run the `cat` command to verify the contents of the `result.txt` file.

```bash
cat result.txt
```

As you can see below, the output of the `ls -lR` command is appended to the end of the _result.txt_ file.

![Appending output to existing files](https://adamtheautomator.com/wp-content/uploads/2022/09/image-828.png)

Appending output to existing files

## **Using the `tee` Command in Conjunction with `sudo`**

Many commands require elevated privileges or `sudo` access, like performing modifications inside the _/etc_ directory. The question is, does the `tee` command work hand in hand with `sudo`? Yes!

Prepending sudo to the tee command allows a non-root user to write data to a file that only a root user can modify. For instance, you want to enable the user _ata_ to write data to the _/etc/ata.conf_ file. But, at the same time, you don’t want to give the user _ata_ root privileges.

1\. Login to your machine with a non-root user.

2\. Next, run the following command without sudo to write the text data to the /etc/ata.conf file.

```bash
echo "data" > tee /etc/ata.conf
```

As you can see below, you’ll get a Permission denied error since _ata_ user doesn’t have write access to the _/etc/ata.conf_ file.

![Modifying a file without prepending the sudo command](https://adamtheautomator.com/wp-content/uploads/2022/09/image-829.png)

Modifying a file without prepending the sudo command

3\. Run the same command below as you did in step two to write the text data to the /etc/ata.conf file. But this time, prepend the sudo command to the tee command. Doing so allows the user _ata_ to complete the command (elevated) as a non-root user.

```bash
echo "data" | sudo tee /etc/ata.conf
```

![Allow ata user to write data to the /etc/ata.conf file, ](https://adamtheautomator.com/wp-content/uploads/2022/09/image-830.png)

Allow ata user to write data to the _/etc/ata.conf_ file,

Lastly, run the cat command below to verify that the _ata_ user could write the text “data” to the /etc/ata.conf.

```bash
cat /etc/ata.conf
```

![Verifying that ata user was able to write to the /etc/ata.conf file](https://adamtheautomator.com/wp-content/uploads/2022/09/image-831.png)

Verifying that _ata_ user was able to write to the /etc/ata.conf file

## Conclusion

Throughout this tutorial, you’ve learned how to use the Unix tee command to write data to files and how it works in conjunction with other commands. At this point, you should have a powerful combination of commands you can use to manage input and output on the Linux command line environment.

You’re like a sushi chef, completely controlling the ingredients and proportions that go into each dish. You can customize each order on the fly to suit your customer’s tastes. The only limit is your imagination (and maybe the size of your terminal window).

Now, go forth and create some amazing command line [concoctions](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html)!

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Funix-tee%2F&text=Master%20Unix%20tee%20Commands%20for%20Real-World%20Linux%20Admin%20Tasks)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Funix-tee%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Funix-tee%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Useful-Linux-Dig-Examples-for-the-Network-Admin.jpg)

### [Linux Dig for Network Admins: Practical Guide with Examples](/linux-dig/)

Become a network troubleshooting expert! This comprehensive Linux Dig guide empowers you with essential DNS query commands for immediate results.

![](https://adamtheautomator.com/wp-content/uploads/2021/12/install-phpmyadmin.jpg)

### [Step-by-Step Tutorial to Install phpMyAdmin Securely on Linux](/install-phpmyadmin/)

Discover how to securely install phpMyAdmin on Linux with our concise guide. Learn essential steps for a safe and efficient setup, tailored for Linux users.

![](https://adamtheautomator.com/wp-content/uploads/2023/08/timedatectl.jpg)

### [Learning the timedatectl Command Though Examples](/timedatectl/)

Learn how to use the Linux timedatctl command-line tool to configure and set your systems time and date in this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
