Most efficient way to grep files in directories for deletion using bash/aliases

北城余情 提交于 2019-12-11 07:49:19

问题


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.



来源:https://stackoverflow.com/questions/52652831/most-efficient-way-to-grep-files-in-directories-for-deletion-using-bash-aliases

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!