用 find -exec 批量删除文件

find 配合 rm -f {} + 一次删除所有匹配文件,不会出现参数列表过长错误。

Linux文件find 文件查找高风险
运行 find ./logs -name "*.tmp" -type f -exec rm -f {} +,即可分批删除 ./logs 下所有 .tmp 文件。

01命令

命令
$ find ./logs -name "*.tmp" -type f -exec rm -f {} +

02作用说明

find 自己生成匹配列表,-exec rm -f {} + 会把文件分批传给 rm,避免在百万级文件的目录上直接 rm *.log 导致的“参数列表过长”错误。加号写法比分号写法高效得多,后者每个文件都要启动一次 rm。执行前先去掉 -exec rm -f {} + 单独运行 find,预览将要删除的文件。

03风险

风险高风险
!注意: 务必先预览:先运行不带 -exec rm -f {} + 的命令查看文件列表;路径或 -name 模式写错可能瞬间删除大量文件。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • find(1) — man7.org
    man7.org · community · 2026-09-17
    依据: Man page documents the -exec action, the {} placeholder, the + form that batches matches into one invocation, and -delete
  • find(1) — Ubuntu manpage
    Ubuntu · community · 2026-09-17
    依据: Ubuntu manpage describes -exec and -execdir actions with {} placeholder and batching with +

最后验证: 2026-09-19 · 分类: 文件与文件夹

06相关方案