How to Save Output of Command in a File in Linux?
Last Updated :
24 Sep, 2024
Linux has a robust command-line interface (CLI) that makes system interaction efficient for users. Saving a command’s output to a file is among the most frequent activities in Linux. When working with big outputs, generating logs, or needing to save command results for later use, this is quite helpful. When we run a command on a terminal on Linux it generates some output of that command. Sometimes we need the output result of the commands. Today we are going to see how to save the output of the command.
Here, we’ll explore all the essential steps and requirements to Save the Output of a Command in a File in Linux effectively.
How to Save Output of Command in a File in Linux?
There are multiple methods available in Linux for saving command output to a file. Some of the more popular methods are listed below:
Using Redirection Operators in Linux
We can use the redirections operators to save the output of commands into files. Redirection operators redirect the output of a command to the file instead of the output terminal.
There are two main redirection operators in Linux:
Example 1: Using the > Operator
The first operator is “>” which is used to redirect the output of a command to the file, but this operator erases all existing data in that file and overwrites the command output,
Let’s see one example of > redirection operator:

In the above image, we can see that the previous content of the output.txt file gets erased and the output of the ls command is written into the file.
Example 2: Using the >> Operator
Now let’s see about the second redirection operator is >>. Using this operator we can append the output of the command to the file. It does not erase any previous content of the file.
Let’s see the example of the>> operator.

In the above image, we can see that the output of the command is appended to the output.txt file without erasing the content stored in that file.
If the file mentioned after the Redirection operator in not exist then it will be created automatically.
Using tee command
When we use the redirection operator the output goes to the file only the redirection operator does not print it on the terminal. To see the output on the terminal and also save it into the file we can use the tee command. It sends the output of the file as well as to the terminal.
First, let’s see how to use the tee operator using the pipeline. Following is the syntax of using the tee operator:
command | tee outfile.txt
Now let’s see one example of the tee command:

We can see in the above image the output is redirected to the terminal as well as the output.txt file.
To append the output of the command to the file use the –a option with the tee command. Here is the syntax of the command:
command | tee outfile.txt
Now Let’s see one example of this command

In this image, we can see that the output of the cowsay command is stored in the output.txt file without erasing the existing data of the file.
Conclusion
One simple but effective feature in Linux is the ability to save command output to a file. Redirecting output is a useful ability whether you’re writing logs, troubleshooting, or just storing data for later use. You can collect errors, merge outputs, and append to or rewrite files based on your needs. You may improve the effectiveness and organization of your workflow by learning these strategies.
Also Read
How to Save Output of Command in a File in Linux – FAQs
How can I write a Linux command’s output to a file?
To redirect the output to a file instead of the screen, use the >
operator followed by the file name. If the file doesn’t exist, it will be created. If it exists, its contents will be replaced.
How do I save the output of a command prompt to a text file?
Open the Command Prompt. Right-click and select “Run as administrator.” To save the output, replace "c:\PATH\TO\FOLDER\OUTPUT.txt"
with your desired file path and replace "YOUR-COMMAND"
with the command you want to run. The command will look like:
YOUR-COMMAND > c:\PATH\TO\FOLDER\OUTPUT.txt
How can I store a command’s output?
You can store a command’s output using redirection characters. By placing the redirection character (>
or >>
) at the end of the command, followed by the file name, you can save the output. Ensure there’s a space between the file name and the redirection character.
Similar Reads
How to Save Output of Command in a File in Linux?
Linux has a robust command-line interface (CLI) that makes system interaction efficient for users. Saving a command's output to a file is among the most frequent activities in Linux. When working with big outputs, generating logs, or needing to save command results for later use, this is quite helpf
4 min read
How to View the Content of File in Linux | cat Command
The cat command in Linux is more than just a simple tool, it's a versatile companion for various file-related operations, allowing users to view, concatenate, create, copy, merge, and manipulate file contents. Let's see the details of some frequently used cat commands, understanding each example alo
7 min read
How to Create a Text File Using the Command Line in Linux
The Linux command line is a powerful tool that allows you to manage your system with precision and speed. One of the most common tasks is creating a text file, which can be achieved using several commands and methods. Here we will learn some quick ways to create a text file from the Linux Command Li
6 min read
How to Append Text to End of File in Linux?
On Linux, while working with files in a terminal sometimes we need to append the same data of a command output or file content. Append means simply add the data to the file without erasing existing data. Today we are going to see how can we append the text in the file on the terminal. Using >>
2 min read
How to Save and Exit in Nano Editor in linux
Saving and exiting in the Nano text editor in Linux is a fundamental skill for anyone working with text files in the terminal. Nano is a user-friendly and straightforward text editor, making it an excellent choice for both beginners and experienced Linux users. Whether you're editing configuration f
5 min read
How to Open a File in Linuxâ
In Linux, a file is a fundamental unit of storage, representing everything from documents and images to system logs and program data. Unlike traditional operating systems, Linux treats almost everythingâfiles, directories, devices, and processesâas a file. Whether you're accessing a simple text docu
6 min read
How to Compress Files in Linux | Tar Command
File compression is a fundamental task in managing and transferring data efficiently on a Linux system. The Tar command, short for Tape Archive, is a powerful tool that allows users to create compressed and archived files. In this comprehensive guide, we will explore the various options and examples
11 min read
How to Print the Longest Line(s) in a File in Linux
Text files are frequently processed while using the Linux command line. This article will go over how to determine which lines in a file are the longest. we will use some commands like awk and grep to achieve our goal to print lines with the longest length. When working with enormous log files. Each
4 min read
How to Get the Full Path of a File in Linux
While dealing with files on Linux, especially shell scripts, one may require determining the full path of a file at times. Now, let's consider several methods of getting the full path of a file in Linux. In this article, we will be discussing several different solutions to a popular problem. Before
3 min read
How to Run a File in Linux
The command line is one of the most powerful tools in Linux. It allows you to execute commands, manage files, and automate tasks all from a single terminal window. One common task you'll often need to do is run a file, whether itâs a script, a compiled program, or even a text file. In this article,
6 min read