使用bash / aliases删除目录中grep文件的最有效方法 [英] Most efficient way to grep files in directories for deletion using bash/aliases

查看:104
本文介绍了使用bash / aliases删除目录中grep文件的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Debian虚拟专用服务器,该服务器托管多个虚拟域和用户。配置它是为了托管多个域,并且每个域名下都有多个电子邮件地址(用户或帐户)。

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.

我的确经常使垃圾邮件安静下来,并且没有时间研究 SpamAssasin 如何过滤某些垃圾邮件基于电子邮件正文中匹配字符串的电子邮件。

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.

相反,以下功能已添加到 .bash_aliases 中,并为方便快速访问而使用了别名

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' &

脚本可以完成任务,但是它确实很慢,而且效率很低。它循环遍历vmail目录中的每个子目录(每个子目录代表一个域名)。然后,它循环遍历每个子目录中的文件,然后搜寻该字符串-如果匹配则将其删除。

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?

推荐答案

您没有提供任何示例输入/输出,因此未经测试,但这听起来像是您要尝试的操作:

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.

这篇关于使用bash / aliases删除目录中grep文件的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆