2014-01-23 101 views
0

我的目录中有几个文本文件需要删除。有在我的目录一堆文本文件:在perl中使用find和rm删除多个文件

leaves_emp1.txt 
pay_emp1.txt 
pf_emp1.txt 
leaves_emp2.txt 
pay_emp2.txt 
pf_emp2.txt 

[...] 

我用下面的代码删除含有“EMP1”文件的尝试:

/usr/bin/find $LogDir -name \"leaves_emp1.txt\" -and -name \"pay_emp1.txt\" -and -name \"pf_emp1.txt\" -exec rm {} 

但它无法正常工作。请帮忙。我尝试了-a和-o。我也尝试使用()作为文件。

+0

我已经用perl重新标记了,因为你显然想在perl脚本中运行这个 - 是吗? – fugu

+0

是的。但我找到了一个办法。我只是使用-name \“* emp1.txt \”。谢谢。 – user3164754

+0

你很迷惑 - 和 - 或。 – Ingo

回答

0

我只是使用的代码如下

/usr/bin/find $LogDir -name \"*emp1.txt\" -exec rm {} 
0

你需要避免叉炸弹。 find中的“exec”调用将为每个文件产生一个fork/exec。我觉得更容易和可扩展的调用是这样的:

find . -type f -name '*emp1.txt' | perl -lane 'unlink $_ if -f $_' - 

注意:如果你想坚持只是EXEC,你必须记得用“\;”在末尾

find . -type f -name '*emp1.txt' -exec rm {} \; 
相关问题