Raspberry Pi

Send Command Output to a File – Raspberry Pi Linux

If you are a Raspberry Pi user then you must have run multiple commands to perform different operations. But as soon as you move to the next command the output of the previous command does not remain saved in a particular file so whenever the terminal is closed the output of commands will disappear. To keep the output of a command saved you have to send that to a file. In this article, we have presented ways to send the output of the command to a file.

How to Send Command Output to a File?

In Raspberry Pi there are multiple ways to send/attach a command’s output to a file, those ways are:

Let us discuss each of them.

1: Sending the Output to a File Directly

To send the command’s output to a file below mentioned syntax can be followed:

Syntax

$ command > output-filename

In the above syntax the command at the left is any command that a user wants to run, and the output of that command will get stored into the output-file. The output filename is the name of the file in which the user wants to store the output of the command, the user can pick any name for this file.

Example

To quote an example let’s suppose I have a file named example-file which consists of the names of different animals. The below cat command is only used to display the content of the file:

$ cat example-file

Now if I have to apply the sort command on it and have to save the sorted result into a separate file, then the below-mentioned command can be used:

$ sort example-file > output-file

Here, the sorted output of example-file is being stored in a file named as output-file

To verify let’s display the content of output-file by using the below-mentioned cat command:

$ cat output-file

As a result of this command, it is clearly visible that the sorted output is saved in our output-file.

2: Saving the Output of a Command Using tee Command

In Linux-based systems the standard output is sent to a file by reading it through the tee command. The syntax of the tee command is shared below:

Syntax

$ command | tee <file_name>.txt

Example

$ date | tee output_file.txt

In this example the output of the first command will be read by tee command and then it will be written on the output_file.

To verify if the data is stored in output_file, we will use the below-mentioned cat command which will display the data present inside the output_file.:

$ cat output_file.txt

3: Appending the Output of Command to a File

If the user wants to append the output of the command to a particular file, then the below-mentioned command can be used to append the data:

Syntax

$ command >> filename

Example

To quote the example let’s create a file named linuxhint_file.txt, to create a file below command will be used:

$ nano <file name>

Note: linuxhint_file is the name of my file users can opt for any other name if they want.

The content that I have added to my file is shown in the image below:

Save the file by pressing keys Ctrl+X then Y and finally press Enter to get back to the terminal.

Now, finally let’s write our append command, for this I am using the echo command:

$ echo "Hello Linux Hint" >> /home/pi/linuxhint_file.txt

In this example, the written message/output of the first echo command is appended to the file which is named linuxhint_file.txt.

Finally, to verify let’s display the content of our file:

$ cat <file-name/path>

In the image below it is clearly visible that the output of our echo command is appended to an already created file instead of saving it to a new file.

Conclusion

The Raspberry Pi system is Linux based which means when a command is run in the Raspberry Pi system the output of it displays on the terminal/screen. But sometimes users feel the need to send the output to a file to keep records or for any other purpose. For that case in the article, we have shared the methods to send and append the output of a command to a file.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.