UNIX 查找:与 -newer 选项相反的选项是否存在?
我知道 unix 的 find 命令有这个选项:
find -version
GNU find version 4.1
-newer file Compares the modification date of the found file with that of
the file given. This matches if someone has modified the found
file more recently than file.
是否有一个选项可以让我查找比某个文件更旧的文件。我想删除目录中的所有文件进行清理。因此,我可以找到所有早于 N 天的文件的替代方案也可以完成这项工作。
I know there is this option for unix's find command:
find -version
GNU find version 4.1
-newer file Compares the modification date of the found file with that of
the file given. This matches if someone has modified the found
file more recently than file.
Is there an option that will let me find files that are older than a certain file. I would like to delete all files from a directory for cleanup. So, an alternative where I would find all files older than N days would do the job too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
!
来否定 -newer 操作,如下所示:如果您想查找上次修改时间超过 7 天前的文件,请使用:
UPDATE:
以避免匹配在您要比较的文件上使用以下内容:
UPDATE2(几年后):
以下内容更复杂,但确实执行严格早于匹配的操作。它使用
-exec
和test -ot
根据比较文件测试每个文件。仅当第一个(测试
)成功时才执行第二个-exec
。删除echo
以实际删除文件。You can use a
!
to negate the -newer operation like this:If you want to find files that were last modified more then 7 days ago use:
UPDATE:
To avoid matching on the file you are comparing against use the following:
UPDATE2 (several years later):
The following is more complicated, but does do a strictly older than match. It uses
-exec
andtest -ot
to test each file against the comparison file. The second-exec
is only executed if the first one (thetest
) succeeds. Remove theecho
to actually remove the files.您可以只使用否定:
您也可以尝试
-mtime
/-atime
/-ctime
/-Btime
选项系列。我不记得它们是如何工作的,但它们在这种情况下可能很有用。请注意从
find
操作中删除文件,尤其是以 root 身份运行的操作;同一系统上的非特权恶意进程可以通过多种方式诱骗它删除您不想删除的内容。我强烈建议您阅读整个“删除文件” GNU 查找 部分手册。You can just use negation:
You might also try the
-mtime
/-atime
/-ctime
/-Btime
family of options. I don't immediately remember how they work, but they might be useful in this situation.Beware of deleting files from a
find
operation, especially one running as root; there are a whole bunch of ways an unprivileged, malicious process on the same system can trick it into deleting things you didn't want deleted. I strongly recommend you read the entire "Deleting Files" section of the GNU find manual.如果您只需要早于文件“foo”的文件而不是 foo 本身,请使用否定按名称排除该文件:
If you only need files that are older than file "foo" and not foo itself, exclude the file by name using negation:
请注意,较新的否定意味着“较旧或相同的时间戳”:
正如您在本例中看到的,还返回相同的文件:
Please note, that the negation of newer means "older or same timestamp":
As you see in this example, the same file is also returned:
不幸的是,find 不支持这个
! -较新并不意味着较旧。它仅意味着不更新,但它也匹配具有相同修改时间的文件。所以我宁愿用
Unfortunately, find doesnt support this
! -newer doesnt mean older. It only means not newer, but it also matches files that have equal modification time. So I rather use