On Linux systems there are quite a few ways to save command line output to files. Each one operates differently and saves different things.
These commands are useful across all Linux distributions and can even be used on Macs and BSD. The only thing you have to think about is which command is best to get the job done.
Unix and Pipes
Before you dive into the commands, you should know a little history. None of this is specific to Linux. It actually comes from the old Unix operating system developed by Bell Labs. Linux, Mac, and BSD are all Unix-like operating systems that can trace back their family tree to Unix.
One of the core ideas behind Unix was to make everything out of small programs that you can string together to do bigger and more complex things. That’s why you can combine Linux commands to make them work together.
There are a couple of ways to pull together Linux (and Unix) commands and make them work together. The most common one you’ll encounter is the
| character. The single pip, as it’s called, passes the output of one command to another.
You’ll also come across the
|& operator. It passes both output and errors to the next command.
Every method in this guide is essentially just a way for Unix commands to pass around information. Instead of piping data between commands, they direct data to output. In this case, that output is a file.
Before you go any further, there are a few things to set up to make testing easier. As your root user, create a couple of folders in your regular user’s home directory. Change their permissions to make them unreadable.
sudo mkdir Fail1 sudo mkdir Fail2 sudo chmod 000 Fail1 sudo chmod 000 Fail2
You’ll be using the
du command to test, and it will raise errors when it encounters those files.
Redirecting Output without Errors
The most common and basic way to redirect output from the terminal into a file is to use the
>> operators. They direct only the output to a specified text file. They don’t redirect errors. Those will still appear in the terminal.
> operator redirects output to a file and overwrites the contents of that file.
du -h ./* > folder-size.txt
In contrast, the
>> operator works the same way but appends the output to the end of the file.
du -h ./* >> folder-size.txt
Saving Only Errors
These next two operators are the exact opposite of the first ones. They save only errors, while they display the output in the terminal.
2> operator sends the errors from a command to a log file, overwriting it. The regular output will still display in the terminal.
du -h ./* 2> errors.log
To achieve the same results without overwriting, use
du -h ./* 2>> errors.log
Save Everything, Show Nothing
If you’re interested in logging everything, the next set of operators has you covered. They redirect all output to files without showing anything in the terminal.
&> operator redirects output and errors to a file and overwrites it.
du -h ./* &> disk_use.log
Like the others, if you don’t want to overwrite, there’s an accompanying operator for that.
du -h ./* &>> disk_use.log
Show Everything, Save Output
You can also display everything in the terminal and only log the output. You can achieve this by piping your command to the
tee acts like a T splitter in plumbing. It directs the flow in two directions. Since the
| operator just passes output, the resulting file will also only contain output and not errors.
du -h ./* | tee output.txt
tee overwrites. To preserve the existing file, add the
du -h ./* | tee -a output.txt
Show and Save It All
Finally, you can redirect with the
|& operator to the
tee command to display everything and log it too.
du -h ./* |& tee output.txt
To avoid overwriting, add the
-a flag to
du -h ./* |& tee -a output.txt
Clearly, there are a ton of options when it comes to redirecting and saving the output of Linux terminal commands to a file. Some of them are more useful than others, but they all can be employed in a wide range of scripts to control the flow of information in the most Unix way possible.