> ## Content Index
> Fetch the complete content index at: https://itsfoss.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Save the Output of a Command to a File in Linux Terminal
- URL: https://itsfoss.com/save-command-output-to-file-linux/
- Published: 2020-11-21T18:07:36.000Z
- Updated: 2023-07-04T06:20:13.000Z
- Description: 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?
- Author: Abhishek Prakash
- Tags: 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](https://itsfoss.com/copy-paste-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](https://linuxhandbook.com/redirection-linux/?ref=itsfoss.com). 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](https://itsfoss.com/content/images/wordpress/2020/11/redirecting_command_output_to_file_in_linux.png)

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](https://itsfoss.com/content/images/wordpress/2020/11/redirecting_command_output_to_file.png)

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](https://linuxhandbook.com/tee-command/?ref=itsfoss.com) 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](https://itsfoss.com/content/images/wordpress/2020/11/display-and-save-linux-command-output.png)

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](https://itsfoss.com/content/images/wordpress/2020/11/pipe-output-to-file-linux.png)

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](https://tldp.org/LDP/abs/html/io-redirection.html?ref=itsfoss.com#FTN.AEN17894) is an essential concept that one should be aware of.

[Linux Command Tutorials for Absolute BeginnersNever used Linux commands before? No worries. This tutorial series is for absolute beginners to the Linux terminal.![](https://itsfoss.com/content/images/size/w256h256/2022/12/android-chrome-192x192.png)It's FOSS![](https://itsfoss.com/content/images/2023/03/terminal-basics-series-tag.png)](https://itsfoss.com/tag/terminal-basics/)

As always, questions and suggestions are always welcome.