UNIX 查找:与 -newer 选项相反的选项是否存在?

发布于 2024-11-17 16:43:00 字数 373 浏览 1 评论 0原文

我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

﹉夏雨初晴づ 2024-11-24 16:43:00

您可以使用 ! 来否定 -newer 操作,如下所示:

find . \! -newer filename

如果您想查找上次修改时间超过 7 天前的文件,请使用:

find . -mtime +7

UPDATE:

以避免匹配在您要比较的文件上使用以下内容:

find . \! -newer filename \! -samefile filename

UPDATE2(几年后):

以下内容更复杂,但确实执行严格早于匹配的操作。它使用 -exectest -ot 根据比较文件测试每个文件。仅当第一个(测试)成功时才执行第二个-exec。删除 echo 以实际删除文件。

find . -type f -exec test '{}' -ot filename \; -a -exec echo rm -f '{}' +

You can use a ! to negate the -newer operation like this:

find . \! -newer filename

If you want to find files that were last modified more then 7 days ago use:

find . -mtime +7

UPDATE:

To avoid matching on the file you are comparing against use the following:

find . \! -newer filename \! -samefile filename

UPDATE2 (several years later):

The following is more complicated, but does do a strictly older than match. It uses -exec and test -ot to test each file against the comparison file. The second -exec is only executed if the first one (the test) succeeds. Remove the echo to actually remove the files.

find . -type f -exec test '{}' -ot filename \; -a -exec echo rm -f '{}' +
回忆凄美了谁 2024-11-24 16:43:00

您可以只使用否定:

find ... \! -newer <reference>

您也可以尝试 -mtime/-atime/-ctime/-Btime选项系列。我不记得它们是如何工作的,但它们在这种情况下可能很有用。

请注意从 find 操作中删除文件,尤其是以 root 身份运行的操作;同一系统上的非特权恶意进程可以通过多种方式诱骗它删除您不想删除的内容。我强烈建议您阅读整个“删除文件GNU 查找 部分手册

You can just use negation:

find ... \! -newer <reference>

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.

请叫√我孤独 2024-11-24 16:43:00

如果您只需要早于文件“foo”的文件而不是 foo 本身,请使用否定按名称排除该文件:

find . ! -newer foo ! -name foo

If you only need files that are older than file "foo" and not foo itself, exclude the file by name using negation:

find . ! -newer foo ! -name foo
情场扛把子 2024-11-24 16:43:00

请注意,较新的否定意味着“较旧或相同的时间戳”:

正如您在本例中看到的,还返回相同的文件:

thomas@vm1112:/home/thomas/tmp/ touch test
thomas@vm1112:/home/thomas/tmp/ find ./ ! -newer test
./test

Please note, that the negation of newer means "older or same timestamp":

As you see in this example, the same file is also returned:

thomas@vm1112:/home/thomas/tmp/ touch test
thomas@vm1112:/home/thomas/tmp/ find ./ ! -newer test
./test
哀由 2024-11-24 16:43:00

不幸的是,find 不支持这个
! -较新并不意味着较旧。它仅意味着不更新,但它也匹配具有相同修改时间的文件。所以我宁愿用

for f in path/files/etc/*; do
    [ $f -ot reference_file ] &&  {
        echo "$f is older" 
        # do something
    }
done

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

for f in path/files/etc/*; do
    [ $f -ot reference_file ] &&  {
        echo "$f is older" 
        # do something
    }
done
私藏温柔 2024-11-24 16:43:00
find dir \! -newer fencefile -exec \
  sh -c '
    for f in "$@"; do
      [ "$f" -ot fencefile ] && printf "%s\n" "$f"
    done
  ' sh {} + \
;
find dir \! -newer fencefile -exec \
  sh -c '
    for f in "$@"; do
      [ "$f" -ot fencefile ] && printf "%s\n" "$f"
    done
  ' sh {} + \
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文