How to Save the Output of a Command to a File in Linux

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.

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.

The most common and basic way to redirect output from the terminal into a file is to use the > and >> operators. They direct only the output to a specified text file. They don’t redirect errors. Those will still appear in the terminal.

The > 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

These next two operators are the exact opposite of the first ones. They save only errors, while they display the output in the terminal.

Using the 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

Redirect Errors To a File

To achieve the same results without overwriting, use 2>>.

du -h ./* 2>> errors.log

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.

The &> operator redirects output and errors to a file and overwrites it.

du -h ./* &> disk_use.log

Redirect everything to a file Linux

Like the others, if you don’t want to overwrite, there’s an accompanying operator for that.

du -h ./* &>> disk_use.log

You can also display everything in the terminal and only log the output. You can achieve this by piping your command to the tee command.

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

By default, tee overwrites. To preserve the existing file, add the -a flag.

du -h ./* | tee -a output.txt

Finally, you can redirect with the |& operator to the tee command to display everything and log it too.

du -h ./* |& tee output.txt

Show Everything and Save Everything Linux

Everything logged from the command Linux

To avoid overwriting, add the -a flag to tee.

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.

Related Ebooks

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.