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

Save Command Line Output To A File 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.

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.

In contrast, the >> operator works the same way but appends the output to the end of the file.

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.

Redirect Errors To a File

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

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.

Redirect everything to a file Linux

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

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.

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

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

Show Everything and Save Everything Linux

Everything logged from the command Linux

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

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.

2 comments

  1. Excellent sequence of commands – I have used most – but how you presented them is much clearer and more organize and easier to remember than my haphazard method. You should do more of this – it is a gift!

Comments are closed.

Sponsored Stories