Often you may need to save output of a command to a file. This is a common requirement especially if you need to run scripts or cronjobs and save their output to files for future reference. There are different ways to do this in Linux. You may append the output to an existing file or overwrite the existing file with latest output or create a new file to save the output. In this article, we will look at how to save command output to file.
How to Save Command Output to File in Linux
For our example, we will write the output of ls command to file /home/data.txt.
1. Using redirect operators
Bash allows you to easily redirect output of any command you run in terminal or shell script or cronjob by simply appending > or >> operator after the command, followed by the file path where you want to copy the command’s output.
- > operator will overwrite the existing content of file with your command’s output. If the file does not exist, it will create a new file.
- >> operator will append your command’s out to the existing content of file. If the file does not exist, it will create a new file.
Basically, both these operators simply redirect standard output that is the screen’s output to your file. Here are some examples of the same.
$ ls > /home/data.txt $ ls >> /home/data.txt
The first command above overwrites the content of /home/data.txt while the second command appends it to the file.
You can check the content of your file with cat /home/data.txt command
Please note, if you do not mention the full file path after redirect operator, the shell will look for the file or create the file in you present working directory.
If you want to repeatedly perform this command simply create a cron job for it. Open crontab in a text editor
$ crontab -e
Add the following line to redirect ls command’s output everyday at 10.a.m.
0 10 * * * sudo ls >/home/data.txt 2>&1
Save and close the file. In the above cronjob we direct output of file to /home/data.txt everyday at 10.a.m. All you need to do is view the file to see that day’s output.
2. Using tee command
When you use redirect operator, the shell will not display the command output on your screen but directly send it to your file. But how do you know if has sent the desired output? For this you need to separately open the file, which can be tedious if you have to do it regularly, or if your file is large and you are appending output using >> redirect operator.
Therefore, sometimes you may want to view command output as well as save it to file. For such use cases, you can use tee command which not only displays the command output on screen but also redirects it to your file. This way you know what you have written to your file.
Here is the syntax of tee command
command | tee /path/to/file
Here is an example
$ ls | tee /home/data.txt
If the above file does not exist, tee command will create a new file and then save command output in it. Here also, if you don’t specify the full file path, it will look for or create the new file in your present working directory.
Please note, the above command will replace the content of your file. If you only want to append the output of your command to your file, you need to use tee command with -a option
$ ls | tee -a /home/data.txt
Like redirect operators above, you can also use tee command in shell scripts & cronjobs.
That’s it. In this article, we have looked at how to save output of Linux command to a file in different ways – using redirect operator and tee command. For both cases, we have also looked at how to replace or overwrite file content with command output.
Also read:
XARGS command to find and delete files
How to Reset Root Password in RHEL/Fedora/CentOS
How to Use Auto Indent in VI Editor
How to Setup LogAnalyzer with Rsyslog & MySQL
How to Setup Rsyslog with MySQL
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.