Most efficient way to grep files in directories fo

2019-07-27 00:06发布

问题:

I have a Debian Virtual Private Server that hosts several virtual domains and users. It was configured so in order to host multiple domains and have multiple email addresses (users or accounts) under each domain name.

I do get spam quiet often, and did not have the time to look on SpamAssasin on how to filter certain emails based on matched string in the email's body.

Instead, the following function was added to the .bash_aliases and it was aliased for quick access use

my_new_del() {
  echo "0:  $1"
  for d in /home/vmail/*/ ; do
      # echo "1:  $d"
      for f in "$d"info/*/*.some.file.pattern*;do
          grep -i -H -l -s "$1" "$f" | while read -r line ; do
              echo "rm -rf $line"
              rm -rf $line
          done
          # echo "2:  $f"
      done
  done
}
alias my_del=my_new_del

Then I use the above alias:

my_del 'some string' &

The script does the job, but it is really slow, and seem inefficient. It loops through each subdirectory in the vmail directory (each subdirectory represents a domain name). Then it loops through the files in each subdirectory and then greps for the string - and deletes it if it gets a match.

Can this be done in a more efficient manner?

回答1:

You didn't provide any sample input/output so this is untested but it sounds like this is what you're trying to do:

my_new_del() {
    find /home/vmail -type f -name '*.some.file.pattern*' -exec grep -i -H -l -s "$1" {} + |
    xargs rm -f {}
}

I'm assuming your file names don't contain any white space since your existing script relies on that assumption.