Bash Write to a File
When we run any command in a bash shell, it generally prints the output of that command to the terminal so that we can read it immediately. But bash also provides an option to ‘redirect’ the output of any bash command to a Log File. It can save the output into a text file so that we can review it later whenever it is needed.
Method 1: Write Output to a File Only
To write the output of Bash commands to a file, we may use right angle bracket sign (>) or double right-angle sign (>>):
Right Angle Bracket Sign (>)
It is used to write the output of bash command to a disk file. If there is no file with the specified name, then it creates a new file with the same name. If the file is there with the specified name, then the content of the file will be overwritten.
Double Right Angle Sign (>>)
It is used to write the output of bash commands to a file, appending the output to the existing contents of the file. If the file is not present, it creates a new one with the specified name.
Technically, both of these operators redirect “stdout (the standard output)” to a file.
In a simple way, when we are writing the file for the first time and do not want previous data to be present in the file, we should use the right angle bracket sign (>). It will overwrite the content if it is already present in the file. And in the further script, we may use double right-angle sign (>>) to append the data to a file.
The ‘ls’ command is used to print all the files and folders present in the current directory. But when we run the ‘ls’ command with a right angle bracket sign (>), it will not print the list of files and folders to the screen. It will save the output to the file that we specify with it, i.e., as shown below:
As shown here, the output of ‘ls’ command is redirected into a file. To print the contents of a file to the terminal, we can use the ‘cat’ command in the following form:
If we want to redirect the output of multiple commands to a single file without deleting the available data, then we can use the >> operator. Suppose we want to append the system information to the specified file, we can do that in the following way:
Here, the result of the second command is appended to the end of the file.
We can repeat this process several times to keep appending the output to the end of the file.
Method 2: Print Output Normally and Write it to a File
Some people may not like writing output to a file using > or >> operators, as there will be no output of the command in the terminal. That is why the ‘tee’ command is used. The ‘tee’ command is used to print the input that it receives to the screen. It can save the output to a file at the same time.
This will override the content of the file, just like the > operator but also print the output on the screen.
If we want to write the output to a file without removing the contents of the file using tee command, we can use the following form which will also print the output to the terminal:
This will not only append the output to the end of the file but also print the output on the screen.