You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用find命令-newer参数时,如何排除指定目录并筛选更新文件

问题场景

当前目录结构及文件时间戳如下:

$ tree -D
.
├── [Jun 25 18:46]  dir1
│	└── [Jun 25 18:51]  file1
└── [Jun 25 17:59]  dir2
    └── [Jun 25 18:46]  file2

2 directories, 2 files

参考文件/tmp/since的时间戳信息:

$ ls -l /tmp/since
-rw-r--r-- 1 xxx None 0 Jun 25 18:45 /tmp/since

使用以下命令时,dir1能被正确排除:

$ find . -name dir1 -prune -o -type f -print
./dir2/file2

但添加-newer参数后,dir1却意外出现在结果中:

$ find . -name dir1 -prune -o -type f -newer /tmp/since
./dir1
./dir2/file2

需要修改命令,实现仅列出修改时间晚于/tmp/since的文件,且不访问dir1目录。


问题原因

find命令的默认动作是:当整个表达式结果为真时,自动执行-print输出。

在出问题的命令中:

  • 匹配到dir1时,-name dir1 -prune这个表达式结果为真(-prune动作的返回值为真),触发了默认的-print,导致dir1被输出;
  • 之前正常的命令里,右边明确指定了-print,左边无输出动作,所以匹配dir1时不会触发输出,只有右边条件满足时才执行输出。

解决方案

只需在右边的表达式末尾加上-print,明确只有满足条件的文件才会被输出,同时保留-prune排除dir1:

find . -name dir1 -prune -o -type f -newer /tmp/since -print

执行结果:

./dir2/file2

也可以用括号明确分组逻辑(需转义括号避免shell解析),效果完全一致:

find . \( -name dir1 -prune \) -o \( -type f -newer /tmp/since -print \)

内容的提问来源于stack exchange,提问作者user3477071

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 18:40:20