In Bash, you can save the output of a command to a file by using the save command. The save command saves the output of a command to a file in the current working directory. The save command can be used to save the output of a command to a file on an remote computer. The save command can also be used to save the output of a command to a file on an local computer. The following example shows how to use thesavecommand to save the output of acommand to a file on an remote computer.
Save the output of ’echo “Hello, world!”’ to ‘/tmp/ echo “Hello, world!”’
Thesavecommand saves the output of ’echo “Hello, world!”’ to ‘/tmp/ echo-world’.
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.
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:
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:
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:
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:
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.