【问题标题】:How to remove files using find and rm command?如何使用 find 和 rm 命令删除文件?
【发布时间】:2015-08-02 07:43:36
【问题描述】:
find -mmin -19 -exec rm '{}'\;

它将首先找到修改过的文件,然后将其删除。 但它给了我如下错误, 发现:`-exec' 缺少参数

还尝试了各种组合,例如,

find -mmin -19 -exec rm '{}';\
find -mmin -19 -exec rm '{}'/;

【问题讨论】:

  • 如果您不使用 GNU 特定的 -delete 选项,请使用空格分隔 {} 和分号,并使用 + 代替分号。

标签: bash shell unix


【解决方案1】:

命令和\;之间需要空格

find -mmin -19 -exec rm {} \;

find已经提供-delete选项,所以你不需要使用-exec rm ..

find -mmin -19 -delete

-删除

删除文件;如果删除成功,则为 true。如果删除失败,则 发出错误消息。如果 -delete 失败,find 的退出状态将 非零(当它最终退出时)。自动使用 -delete 打开 -depth 选项。

警告:不要忘记 find 命令行被评估为 表达式,所以先放 -delete 会使 find 尝试删除 您指定的起点以下的所有内容。当测试一个 查找您稍后打算使用的命令行 -delete,您应该明确指定 -depth 以避免以后出现意外。因为 -delete 意味着 -depth,所以你不能有用 一起使用 -prune 和 -delete。

【讨论】:

  • 使用find ./ -type f -iname \*.jpg -delete 删除 jpg 文件是否安全?
  • 会删除当前目录和子目录下的所有jpg文件。
【解决方案2】:

您缺少将大括号与分号分开的必要空格。

find -mmin -19 -exec rm '{}' \;

但它的作用相同,更容易输入,而且执行速度可能更快。

find -mmin -19 -delete

【讨论】:

  • 它给了我错误 rm: cannot remove .' or ..' rm: cannot remove `./rm1': Is a directory ls -a 。 .. rm1
  • 您不能删除非空目录。如果要删除整棵树,请先让find 运行深度。
  • 在 exec 之前添加 -not -type d 以避免该错误。
  • @tripleee 您根本无法删除具有非递归 rm 的目录
  • @Jasen 但是find -delete 你可以。
【解决方案3】:

这是另一种选择。指定我们要删除文件的日期:

find /SYSADMIT/* -type f -not -newermt "AAAA:MM:DD HH:MI:SS" -delete

摘自:https://www.sysadmit.com/2019/08/linux-borrar-ficheros-por-fecha.html

【讨论】:

    猜你喜欢
    • 2017-08-07
    • 2018-12-28
    • 2021-06-07
    • 2014-09-19
    • 1970-01-01
    • 2020-07-09
    • 2017-03-27
    • 2015-07-24
    • 2016-05-09
    相关资源
    最近更新 更多