Quick Links

When you run a command at the bash prompt, it normally prints the output of that command directly to the terminal so you can read it immediately. But bash also allows you to "redirect" the output of any command, saving it to a text file so you can review the output later.

This works in bash on any operating system, from Linux and macOS to Windows 10's Ubuntu-based bash environment.

Option One: Redirect Output to a File Only

To use bash redirection, you run a command, specify the > or >> operator, and then provide the path of a file you want the output redirected to.

  •  > redirects the output of a command to a file, replacing the existing contents of the file.
  •  >> redirects the output of a command to a file, appending the output to the existing contents of the file.

Technically, this redirects "stdout"---the standard output, which is the screen---to a file.

Here's a simple example. The

        ls
    

command lists files and folders in the current directory. So. when you run the following command, ls will list files and folders in the current directory. But it won't print them to the screen---it will save them to the file you specify.

ls > /path/to/file

You don't have to specify the path to an existing file. Specify any valid path and bash will create a file at that location.

If you view the contents of the file, you'll see the ls command's output. For example, the cat command prints the contents of a file to the terminal:

cat /path/to/file

Remember, the >  operator replaces the existing contents of the file with the output of the command. If you want to save the output of multiple commands to a single file, you'd use the >> operator instead. For example, the following command will append system information to the file you specify:

uname -a >> /path/to/file

If the file doesn't already exist, bash will create the file. Otherwise, bash will leave the existing contents of the file alone and append the output to the end of the file.

When you view the contents of the file, you'll see the results of your second command were appended to the end of the file:

You can repeat this process as many times as you like to keep appending output to the end of the file.

Option Two: Print Output Normally and Redirect It to a File

You might not like redirecting output with the > or >> operators, as you won't see the output of the command in the terminal. That's what the tee command is for. The tee command prints the input it receives to the screen and saves it to a file at the same time.

To pipe the output of a command to tee, printing it to your screen and saving it to a file, use the following syntax:

command | tee /path/to/file

This will replace anything in the file with the output of the command, just like the > operator.

To pipe the output of a command to tee , printing to to your screen and saving it to a file, but appending it to the end of the file:

command | tee -a /path/to/file

This will append the output to the end of the file, just like the >> operator.

Related: The Beginner's Guide to Shell Scripting: The Basics

The bash shell includes some additional, advanced operators that perform similar functions. They'll be particularly useful if you're writing bash scripts. Consult the I/O Redirection chapter in the Advanced Bash-Scripting Guide for more detailed information.