save grep output to file

How to Save Grep Output to File in Linux

Sometimes you may need to write grep output to file in Linux for later use. There are many ways to do this. In this article, we will look at a couple of ways to easily save grep output to file in Linux.


How to Save Grep Output to File in Linux

Here is how to save grep output to file in Linux.


1. Overwrite grep output to file

You can easily write grep output to another file using > operator. Here is the syntax

$ sudo grep [options] search_string old_file_path > new_file_path

In the above statement, you need to mention search string, the file where you want grep to search (old_file_path) and the file where you want grep to write the result (new_file_path).

Here is an example to search “test” in our file /home/data.txt and write to file /etc/result.txt

$ sudo grep "test" /home/data.txt > /etc/result.txt

In the above statement, if you do not specify full file paths grep will look for these files in your present working directory. Also if the destination file result.txt does not exist, it will be newly created. Also, its content will be completely overwritten with the result of grep command.

If you only want to append the grep result to this file, follow the steps below.


2. Append grep output to file

In this case, we will append the result of grep command to new file, instead of overwriting it, using >> operator, instead of using > operator.

$ sudo grep [options] search_string old_file_path >> new_file_path

In the above statement also you need to specify search string, path of the file to be searched (old_file_path) and path of the file (new_file_path) to which you want to append the grep result.

Here is an example to search “test” in our file /home/data.txt and append to file /etc/result.txt

$ sudo grep "test" /home/data.txt >> /etc/result.txt

In the above statement, if you do not specify full file paths grep will look for these files in your present working directory. In this case, the result of grep command will simply be appended to result.txt.

If you want to automate this task, you can simply create a cronjob for it. Here is an example. Open crontab file.

$ crontab -e

Add the following line to run the above grep command every day at 10 am.

0 10 * * * sudo grep "test" /home/data.txt >> /etc/result.txt

Save and close the file. Now, every day at 10am grep command will search data.txt for “test” string and append the output to result.txt file.

In this article, we have looked at how to save output of grep command to file. It is very useful to look for specific strings from files that are updated regularly, such as server logs, and filter the result to another files. You may overwrite or append the result to a file, depending on your requirement.

Also read:

How to Force File Download in PHP
How to Force File Download in NGINX
How to List All Users in Ubuntu
Apache Configure Multiple Virtual Hosts
How to Clean up disk space in Linux

Leave a Reply

Your email address will not be published.