When you run a command in a Terminal window, the output of the command is normally printed to the Terminal, or the screen, so you can read it immediately. You can also print the output of a command to a file, if you want to save it.
Saving the output of a command to a file is helpful if you’re trying to diagnose a problem. To get help from tech support, you may have to run certain commands on your computer and send the results to the support team. This is easy using output redirection on the command line.
Today we’ll cover how to send the output of a command to a text file in the bash shell on Windows, Mac, and Linux.
Redirect Output to a File Only
There are two operators for redirecting the output of a command to a file instead of the screen.
The > symbol creates a new file if not present, or overwrites the file if it already exists. The >> also creates a new file if not present, but it appends the text to the end of the file if it already exists, rather than overwriting the file.
Note to self: when you redirect a shell script output to a file, it won’t be displayed on the console :-) #StupidMe
— Xavier Mertens? (@xme) August 5, 2017
To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory. When you run the following command, the list of files and folders will be written to the ls_output.txt file in the TerminalOutput folder.
Replace /path/to/file with the full path to the file you want to use.
ls > /path/to/file
This is the command for our example.
ls > /Users/lorikaufman/TerminalOutput/ls_output.txt
To view the contents of the file in the Terminal window, use the cat command as shown in the following command. Again, replace /path/to/file with the full path to the file you want to view.
The > operator replaces the contents of an existing file every time you use it to redirect output. If you want to save the output from multiple commands to a single file, use the >> operator instead. This appends the output of a command to the end of the specified file, if it already exists. If the file doesn’t exist, it creates a new one.
For example, we’ll append system information to the end of the file we created above. So, in the following line, uname -a is the command, followed by the >> redirection operator. Then, the full path to the file to which we want to append the output of the uname command.
The -a parameter in the uname command prints all available system information.
uname -a >> /Users/lorikaufman/TerminalOutput/ls_output.txt
To view the file with the appended information in the Terminal window, enter the following command, with the full path to your file.
Repeat this process as many times as you need to keep appending command output to the end of the file.
Here’s another example of sending the output of the ls command as directory tree, with subdirectories listed recursively (the R parameter) and one file per line (the 1 parameter).
ls -1R > /Users/lorikaufman/TerminalOutput/dir_tree.txt
Then, we use the cat command, as previously mentioned to view the contents of the file in the Terminal window.
Print Output to Screen and Redirect It to a File
The > and >> operators don’t display the output of a command on the screen. The output is only sent to a file.
If you want to send the output to a file and see it on the screen, use the tee command instead of the > and >> operators.
Nerd alert! Today I discovered the `tee` command, which pipes to both a file and standard out. Unix continues to delight. pic.twitter.com/aClIT8AdJB
— Zeke Sikelianos (@zeke) August 29, 2017
To use the tee command you must pipe the output from the command you’re running, like ls, to the tee command, using the pipe operator, a vertical bar (|). For example, the following command takes the output of the ls command and pipes it into the tee command. The tee command then sends that output to the screen and to a file you specify using the full path to the file.
ls | tee /Users/lorikaufman/TerminalOutput/ls_output.txt
The above command replaces all the content in the file or creates a new one if it doesn’t exist, just like the > operator. We used the following cat command to view the contents of our file in the Terminal window.
To use the tee command to print output to the screen and append it to end of a file, enter the following command. Here, we’re adding full system information to the end of the file using the uname -a command, just like we did with the >> operator, but sending the output to the screen as well.
uname -a | tee -a /Users/lorikaufman/TerminalOutput/ls_output.txt
The following command is the tee version of printing a directory tree recursively to a file and to the screen, one file on each line, just like we did with the > operator.
ls -1R | tee /Users/lorikaufman/TerminalOutput/dir_tree.txt
Then, we use the cat command again to view the contents of the file.
But Wait, There’s More
There are many more ways to redirect output, including errors, to a file. You can read about the different operators and commands used to redirect output in the I/O Redirection chapter of the Advanced Bash Scripting Guide. You can also learn more about using the command line.
I've learned a good lesson today. Don't redirect the log output of a 10 million iteration for loop to a txt file. Result: 5 gb txt file.
— Lloyd (@lcarswell1) May 21, 2017
What do you use output redirection for? Which method do you use? Have you learned any other ways to redirect output? Share your thoughts and experiences with us in the comments below.