How to pipe output to a file on Linux

When using the Bash shell on a Linux system, we can send the output of a command to somewhere else, by passing it to another command or a file, for example. This is called Bash shell redirection. It is useful in many different scenarios, and is one of the most basic and essential components of learning to use the Bash shell and Bash scripting.

In this tutorial, we will introduce you to the standard Bash shell redirection operators, which allow us to send standard output and/or standard error to a file. You will also learn about the tee command, which can pipe output to a file while simultaneously displaying the output on the terminal screen. Let’s dive into some examples below to see how this works.

In this tutorial you will learn:

  • How to redirect standard output and/or standard error to file
  • How to pipe output to a file with the tee command
How to pipe output to a file on Linux
How to pipe output to a file on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell, tee
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to pipe output to a file on Linux – command examples

Let’s first cover how to redirect standard output to a file. If you already have these Bash basics down, feel free to skip ahead to the example commands.

You normally use > for redirection, and to append output to an existing file, you would use >>. Here we are sending the output of the echo command to different files:

$ echo "" > new-file.txt
$ echo "" >> existing-file.txt

With these commands, standard output would be redirected to the file, and standard error is shown on your screen. In case you are new to Linux, standard output is just the usual, expected output from running a command or script, and standard error is any error messages that occur.

To redirect standard error instead, we can use the 2> operator. This will make standard output appear on our screen, and send all error messages to a file.

$ echo "" 2> new-file.txt

With the basics out of the way, we will now cover various command examples for redirecting standard output and/or standard error to a file, and also see how to pipe output to a file with the tee command.

The built in Bash operators will usually suffice when it comes to redirecting output to a file, but occasionally using the tee command with a pipe is necessary if we need to display the output on our screen simultaneously.
  1. Instead of using the > for normal Bash redirection, you can use a pipe and the tee command without any extra options. You will just need to specify the name of the file for which you want to write standard output. The output will then be shown in your terminal and will also be sent to the file.
    $ echo example output | tee file.txt

    The “example output” text was sent to our terminal as well as file.txt in this example.

  2. You can also send standard output to multiple files at the same time with tee. Simply specify the path to each file name in your command.
    $ echo example output | tee file1 file2 file3

  3. If you also want standard error to be redirected to the file, as opposed to only showing it in terminal, you can add the 2>&1 operator before the pipe to tee.
    $ echo example output 2>&1 | tee file.txt
  4. To append to a file instead of overwriting it, which is equivalent to the Bash >> operator, you can use the -a or --append option with tee.
    $ echo example output | tee -a file.txt
  5. If you want to see standard output and standard error on your screen, while appending them to the same file, we can use the -a (append) option with tee and the 2>&1 operator.
    $ echo "" 2>&1 | tee -a existing-file.txt
  6. To redirect standard output and standard error to the same file with Bash operators, use the following command syntax. Specifically, append 2>&1 to the end of your usual command.
    $ echo "" > new-file.txt 2>&1
  7. A slightly easier way to achieve this functionality is with the &> operator. Note that this works fine in Bash and zsh, but not other shells, so use the previous example for maximum compatibility with other systems.
    $ echo "" &> new-file.txt
  8. To append standard output and standard error to a file that already exists, use the same syntax above, but with the >> redirection operator.
    $ echo "" >> existing-file.txt 2>&1
  9. To redirect standard output to one file, and redirect standard error to a different file, use the following syntax.
    $ echo "" 2> std-err.txt 1> std-out.txt

Closing Thoughts

In this tutorial, we saw how to pipe output to a file on a Linux system. This can be accomplished by using the built in Bash operators > and >>, along with 1>, 2>, and 2>&1 for special scenarios. In addition, we can use the tee command, which provides a different functionality by piping the output (standard output and optionally standard error) to a file, while still displaying the output on your terminal screen. Since these are slightly different approaches, you can use the right tool for the job at hand.