【问题标题】:Need help in unix find with xargs command在使用 xargs 命令查找 unix 时需要帮助
【发布时间】:2015-03-20 08:37:15
【问题描述】:

我正在尝试使用以下 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

你能帮忙解决这个问题吗?

【问题讨论】:

    标签: unix xargs


    【解决方案1】:

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

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

    那么 find 的所有结果都将以 '\0' NUL char 结尾,并且 xargs 将通过使用相同的 char 解析正确地重新生成列表。

    【讨论】:

      【解决方案2】:

      find可以直接对每个结果执行一条命令:

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

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

      【讨论】:

        【解决方案3】:

        这是实现您想要的最快的方法:

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

        注意结尾的+,它的作用与xargs 相同。

        【讨论】:

          猜你喜欢
          • 2014-03-26
          • 2015-05-28
          • 1970-01-01
          • 1970-01-01
          • 2011-11-18
          • 1970-01-01
          • 2010-12-26
          • 1970-01-01
          • 2015-04-12
          相关资源
          最近更新 更多