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

When you run a command or script in the Linux terminal, it prints the output on the screen for your immediate viewing. What if you want to save it to a file?
Warp Terminal

When you run a command or script in the Linux terminal, it prints the output on the screen for your immediate viewing.

There will be times when you need to save the output to a file for future references. Now, you can surely copy and paste in Linux terminal but there are better ways to save the output of a shell script or command in Linux command line. Let me show them to you.

Method 1: Use redirection to save command output to file in Linux

You can use redirection in Linux for this purpose. With redirection operator, instead of showing the output on the screen, it goes to the provided file.

  • The > redirects the command output to a file replacing any existing content on the file.
  • The >> redirect adds the command output at the end of the file's existing content (if any).

Use the STDOUT redirection operator > for redirecting the output to a file like this:

command > file.txt

If the file.txt doesnโ€™t exist, it will be created automatically. If you use the > redirect again with the same file, the file content is replaced by the new output.

The example below demonstrates it better. It first saves the output of ls -l command. And then later, it replaces the content of the file with the output of ls *.c command.

Redirecting Command Output To File In Linux
Redirecting command output to file

If you donโ€™t want to lose the content of the existing file while saving the output of a script or command, use the redirection operation in append mode with >>.

command >> file.txt

This example demonstrates it better:

Redirecting Command Output To File
Redirecting command output to file in append mode

Even here, if the file doesnโ€™t exist, it is created automatically.

Bonus Tip: Save Linux command output as well as error to a file

If your Linux command returns an error, it doesnโ€™t get saved in the file. You can save both the command output and command error in the same file using 2>&1 like this:

command > file.txt 2>&1

Basically, 0 stands for standard input, 1 for standard output and 2 for standard error.

Here, you are redirecting (>) standard error (2) to the same address (&) as standard output (1).

Method 2: Use tee command to display the output and save it to a file as well

By the way, did you notice that when you send the command output to a file, you cannot see it anymore on the display? The tee command in Linux solves this problem for you.

Like a tee pipe that sends a water stream in two directions, the tee command sends the output to the display as well as to a file (or as input to another command). You can use it like this:

command | tee file.txt

Again, the file will be created automatically if it doesnโ€™t exist already.

You may also use the tee command in append mode with option -a in this manner:

command | tee -a file.txt

Let me demonstrate it with some easy-to-follow examples:

Display And Save Linux Command Output

I have used simple Linux commands in my examples. But rest assured; you can use these methods to save the output of bash scripts as well.

Note: Avoid pipe pitfall while saving command output to a file

You probably are familiar with pipe redirection. You may use it to combine Linux commands but you cannot pipe the output to a file. It will result in an error that filename command not found:

Pipe Output To File Linux

This is because the pipe redirects one command's output to another command's input. And in this case, you give it a file name while it is expecting a command.

If you are new to the Linux command line, I hope this quick tutorial added to your Linux knowledge a bit. I/O redirection is an essential concept that one should be aware of.

Linux Command Tutorials for Absolute Beginners
Never used Linux commands before? No worries. This tutorial series is for absolute beginners to the Linux terminal.

As always, questions and suggestions are always welcome.

About the author
Abhishek Prakash

Abhishek Prakash

Created It's FOSS 11 years ago to share my Linux adventures. Have a Master's degree in Engineering and years of IT industry experience. Huge fan of Agatha Christie detective mysteries ๐Ÿ•ต๏ธโ€โ™‚๏ธ

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

It's FOSS

Great! Youโ€™ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.