When you run a terminal command, it will typically print output in the terminal, so you can read it immediately. However, you’ll sometimes want to save the output to analyze it later or combine it with another tool.
You can do this using output redirection on the command line. Find out how to send the output of a command to a text file on Windows, Mac, and Linux.
Redirect Command-Line Output to a File
There are two operators you can use to redirect the output of a command to a file: >> and >. It's important you understand the difference between the two to avoid unintended data loss.
The > symbol creates a new file or overwrites it if it already exists. The >> operator also creates a new file if it doesn't exist, but it won’t overwrite an existing file. If the file already exists, it will append the text to the end of it.
To redirect the output of a command to a file, type the command, then specify the > or >> operator, and finally provide the path to a file you want to redirect the output to. For example, here’s how you can save the output from the ls command, which lists the contents of a directory:
ls > /path/to/file
Replace /path/to/file with the full path to the file you want to use. The command will run silently, storing the output in the file you specify.
To view the contents of the file in your terminal, you can use the cat command. Again, replace the /path/to/file with the full path to the file you want to view.
cat /path/to/file
You should see the output from the command in your new file:
The > operator replaces the contents of an existing file. If you want to save the output from several commands to a single file, use the >> operator instead. This appends to the file, so you won't lose any previous output you've saved.
For example, try appending system information to the end of the file you just created. Simply run uname -a on Linux/Mac—or the ver command if you're on Windows—and add the >> operator along with the path to the file:
uname -a >> /path/to/file
Repeat this process as many times as you need to keep appending command output to the end of the file.
Print Output to Screen and Redirect It to a File
The > and >> operators don't display the output of a command on the screen, they just send it to a file. If you want to send the output to a file and see it on the screen, use the tee command.
To use the tee command, send the output from another command to it using the pipe operator, a vertical bar (|). For example, here's how you can send the output of the ls command to tee using a pipe:
ls | tee /path/to/output.txt
The tee command then sends that output to the screen and to the file you specify. This will overwrite the file or create a new one if it doesn't exist, just like the > operator.
To use the tee command to print output to the screen and append it to the end of a file, add the -a flag before the file path, for example:
uname -a | tee -a /path/to/output.txt
You'll see the command output on your screen and you can use cat to verify that tee has also added it to the file:
Take Advantage of the Command Line’s Power
There are many more ways to redirect output, including errors, to a file. With the power of the command line, you can monitor system activity, combine data from several sources, and more.
Besides saving output, there's much more you can do on the terminal. While the learning curve can be steep, terminal commands are expressive, composable, and often quicker than GUI alternatives.