在使用xargs命令查找unix时需要帮助

时间:2015-01-21 17:57:53

标签: unix xargs

尝试使用以下find命令删除文件时,很少有文件没有删除,所有这些文件名都是:filname.123.log。

我无法重命名或无法对文件名执行任何操作只需删除即可 命令

$ find /BASE/CODE/LOGS_BACK -type f -mtime +60 | xargs rm -f
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

我用谷歌搜索并尝试使用以下命令,但它给出了不同的错误。

$ find /BASE/CODE/LOGS_BACK -type f -mtime +60 | xargs -0 rm -f
xargs: argument line too long

你可以帮忙解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

在xargs中使用-0选项意味着在find:

中使用-print0选项

find / BASE / CODE / LOGS_BACK -type f -mtime +60 -print0 | xargs -0 rm -f

然后查找的所有结果将以'\ 0'NUL char结束,xargs将通过使用相同的char解析来正确重新生成列表。

答案 1 :(得分:0)

find可以直接为每个结果执行命令:

$ find /BASE/CODE/LOGS_BACK -type f -mtime +60 -exec rm -f {} \;

{}将替换为每个匹配的文件名。 \;结束命令。

答案 2 :(得分:0)

这是实现目标的最快方式:

find /BASE/CODE/LOGS_BACK -type f -mtime +60 -exec rm -f {} +

请注意与+执行相同工作的结尾xargs