0

I have a command I'm using to find all of the *.xml files on our server. I'm using:

find -type f -exec grep -il "xml" {} \;

This is great, but there are maybe a thousand files like this on the server, and my window fills up.

I would like to be able to generate a report and have it save somewhere on the server or even email to me.

I would also like to specify to look only in the last 5 days when it generates this report.

techraf
  • 5,941
  • Redirect output to a file: find -type f -exec grep -il "xml" {} \; > resultfile.txt. Then, read the file either with less resultfile.txt or any text editor of your choice. –  Apr 18 '16 at 02:16

1 Answers1

2

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:

  1. 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.

  1. 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.

  1. To tell find to only report files that were modified in the past 5 days, use -mtime -5.

  2. 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

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • WOW! Thank you Jeff so much for your thorough answer. I'm going to spend some time experimenting with this. – Markus Alison Apr 18 '16 at 23:50
  • One other question. I really like this forum, specifically the signup with facebook/google+. Is it a standalone forum that I can purchase somewhere and use? – Markus Alison Apr 18 '16 at 23:50
  • Take your time with the answer and let me know if any of it is unclear. If it solves your problem, indicate it by clicking the check mark. If another answer comes in that fits better, feel free to select it instead. As to the site/standalone question, I can't say; ask on the meta site. – Jeff Schaller Apr 18 '16 at 23:59
  • Ok, I tried this... – Markus Alison Apr 19 '16 at 00:57
  • Ok, I had some issues with some of that. I ended up using... find / -type f -name '*.xml' > /home/detector565/public_html/report.txt And that did it! – Markus Alison Apr 19 '16 at 02:29