Let's start with saving the file somewhere first.
Take your command:
find -type f -exec grep -il "xml" {} \;
and read through: What are the shell's control and redirection operators? to where it says:
>
: Directs the output of a command into a file.
to make your command something like:
find -type f -exec grep -il "xml" {} \; > /tmp/report.txt
I'll take this opportunity to recommend a couple tweaks to your current find
command:
Might be a personal preference (or legacy memory on my part), but I like to specify the starting directory for find
, like so:
find / -type f ....
so that it doesn't matter where you start the command or script from, it will always produce the expected results.
- Your
find
command is not searching for "*.xml" files, it's searching for files that contain the (case-insensitive) string "xml". If contents-searching is what you want, then stop reading this paragraph. If you actually want to find files that have "xml" in their filename, then look for something like: find / -type f -iname '*xml*'
-- where iname
tells find
to match for filenames that contain (case-insensitive) "xml". If you only care about "xml" being at the end of the filename, then use find / -type f -iname '*xml'
or ... -iname '*.xml
, if you want a period before the xml.
2a. If you are wanting to search the contents of the files, consider changing your general-case grep
to one that tells grep that you're searching for a fixed string: ... grep -Fil xml ...
to speed things up a tiny bit.
To tell find to only report files that were modified in the past 5 days, use -mtime -5
.
Since you tagged the Q as Linux, I'll assume that you have GNU find (man find
and look for "GNU" in the Description to confirm). In that case, can can speed the process up a bit more by telling find
to pass in multiple files to each exec by using this syntax:
find / -type f -mtime -5 -exec grep -Fil xml {} + > /tmp/report.txt
find -type f -exec grep -il "xml" {} \; > resultfile.txt
. Then, read the file either withless resultfile.txt
or any text editor of your choice. – Apr 18 '16 at 02:16