How to Save Command Line Output to File on Windows, Mac, and Linux
Pinterest Stumbleupon Whatsapp
Advertisement

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.

Note: We’ll be using the Mac Terminal These 6 Awesome Terminal Commands Will Boost Your MacBook These 6 Awesome Terminal Commands Will Boost Your MacBook You can only cram so much into graphical user interface before it becomes cluttered, so it should come as no big surprise that you can do some really cool things using the Mac Terminal. Read More in our example, but it works the same way in the bash shell on Linux 40+ Most Used Linux Terminal Commands 40+ Most Used Linux Terminal Commands Whether you're just getting started or simply curious about the Linux terminal, here are the most common commands that will carry you through your entire time on Linux. Read More and in the new bash shell in Windows 10 A Quick Guide to Linux Bash Shell in Windows 10 A Quick Guide to Linux Bash Shell in Windows 10 You can now run Linux on Windows. Learn more about Bash on Windows, from how and why you should install it, to hidden features you probably didn't know about. Read More .

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.

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.

cat /path/to/file

save command line output windows mac linux

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.

cat /Users/lorikaufman/TerminalOutput/ls_output.txt

Repeat this process as many times as you need to keep appending command output to the end of the file.

save command line output windows mac linux

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.

cat /Users/lorikaufman/TerminalOutput/dir_tree.txt

save command line output windows mac linux

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.

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.

cat /Users/lorikaufman/TerminalOutput/ls_output.txt

save command line output windows mac linux

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

save command line output windows mac linux

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.

cat /Users/lorikaufman/TerminalOutput/dir_tree.txt

save command line output windows mac linux

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 A Quick Guide To Get Started With The Linux Command Line A Quick Guide To Get Started With The Linux Command Line You can do lots of amazing stuff with commands in Linux and it's really not difficult to learn. Read More .

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.

Leave a Reply

Your email address will not be published. Required fields are marked *