Linux / Sendmail的一个class轮有大型目录的麻烦

无论出于何种原因,垃圾邮件发送者都find了通过侧面系统中继邮件的方法。 这个漏洞已经被解决了。

问题是我的/ var / spool / mqueue目录中有大量的电子邮件(至less100,000+),我仍然需要过滤。 我停止了sendmail,并将mqueue目录的内容移动到新的位置…

从那以后,我一直试图用下面的一个class轮来帮忙:

for x in `find . -type f -name “qf*” | xargs grep -l "foo" | cut -b3-`; do y=d`echo $x | cut -b2-`; mv $x /root/spammessages; mv $y /root/spammessages/; done 

这个想法是:

1)检查QF文件的唯一垃圾邮件相关的标题内容(foo)。

2)findDF对应文件

3)将df和qf文件移至隔离区域。

问题是,查询正在运行,但不希望移动任何文件。 如果我跑顶,我会偶尔看到xargs和grep使用一些资源,但不会超过1%-2%。 此外…当我检查spammessages文件夹,我没有看到任何文件。

如果我在一个较小的消息子集上运行相同的命令,它似乎工作正常。 这里有一些Linux文件的限制吗? 有没有方法来优化单线?

谢谢。

-M

让我们分成多行

 for x in `find . -type f -name "qf*" | xargs grep -l "foo" | cut -b3-` do y=d`echo $x | cut -b2-` mv $x /root/spammessages mv $y /root/spammessages/ done 
  • 当你将文件名从一个文件pipe道传送到另一个文件时,你应该总是使用-print0find-0
  • 使用$()代替反引号来提高可读性,并且可以嵌套和减less对转义的需求
  • 始终引用保存文件名的variables名称
  • 在一个循环中多次调用一个外部( cut )是很慢的(我已经消除了它,因为它不是必须的)
  • 您在一个目录上有一个terminal斜杠,但不是另一个(一致性)
  • 当你做variablesx ,你将切断“qf”,但是当你做这个动作的时候,你不会把它放回去。
  • 你正在切断“qf”,但是当你做variablesy (你正在从x删除一个额外的字符)不会放回“f”
  • 另外,你在你的filespec文件中find可能受到干扰的文字引号(智能或Unicode或任何你想要调用它们的文件)

尝试这个:

 for x in $(find . -type f -name "qf*" -print0 | xargs -0 grep -l "foo" | cut -b3-) do mv "qf$x" /root/spammessages/ mv "df$x" /root/spammessages/ done 

放回一行:

 for x in $(find . -type f -name "qf*" -print0 | xargs -0 grep -l "foo" | cut -b3-); do mv "qf$x" /root/spammessages/; mv "df$x" /root/spammessages/; done 

编辑:

这是一个使用while循环的版本,对于大量的文件可能会更好:

 find . -type f -name "qf*" -print0 | xargs -0 grep -l "foo" | cut -b3- | while read -rx do mv "qf$x" /root/spammessages/ mv "df$x" /root/spammessages/ done 

在一行中:

 find . -type f -name "qf*" -print0 | xargs -0 grep -l "foo" | cut -b3- | while read -rx; do mv "qf$x" /root/spammessages/; mv "df$x" /root/spammessages/; done 

一个使用Bash过程replace的变体:

 while read -rx do mv "qf$x" /root/spammessages/ mv "df$x" /root/spammessages/ done < <(find . -type f -name "qf*" -print0 | xargs -0 grep -l "foo" | cut -b3-) 

和:

 while read -rx; do mv "qf$x" /root/spammessages/; mv "df$x" /root/spammessages/; done < <(find . -type f -name "qf*" -print0 | xargs -0 grep -l "foo" | cut -b3-)