如何查找最近x分钟内修改过的文件(查找-mmin不能按预期工作) [英] How to find files modified in last x minutes (find -mmin does not work as expected)

查看:224
本文介绍了如何查找最近x分钟内修改过的文件(查找-mmin不能按预期工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找最近x分钟(例如最近一小时)内修改过的文件.网上的许多论坛和教程都建议将find命令与-mmin选项一起使用,如下所示:

I'm trying to find files modified in last x minutes, for example in the last hour. Many forums and tutorials on the net suggest to use the find command with the -mmin option, like this:

find . -mmin -60 |xargs ls -l

但是,此命令对我不起作用.从下面的清单中可以看到,它还显示了早于1小时之前修改的文件:

However, this command did not work for me as expected. As you can see from the following listing, it also shows files modified earlier than 1 hour ago:

-rw------- 1 user user   9065 Oct 28 23:13 1446070435.V902I67a5567M283852.harvester
-rw------- 1 user user   1331 Oct 29 01:10 1446077402.V902I67a5b34M538793.harvester
-rw------- 1 user user   1615 Oct 29 01:36 1446078983.V902I67a5b35M267251.harvester
-rw------- 1 user user  72365 Oct 29 02:27 1446082022.V902I67a5b36M873811.harvester
-rw------- 1 user user  69102 Oct 29 02:27 1446082024.V902I67a5b37M142247.harvester
-rw------- 1 user user   2611 Oct 29 02:34 1446082482.V902I67a5b38M258101.harvester
-rw------- 1 user user   2612 Oct 29 02:34 1446082485.V902I67a5b39M607107.harvester
-rw------- 1 user user   2600 Oct 29 02:34 1446082488.V902I67a5b3aM465574.harvester
-rw------- 1 user user  10779 Oct 29 03:27 1446085622.V902I67a5b3bM110329.harvester
-rw------- 1 user user   5836 Oct 29 03:27 1446085623.V902I67a5b3cM254104.harvester
-rw------- 1 user user   8970 Oct 29 04:27 1446089232.V902I67a5b3dM936339.harvester
-rw------- 1 user user 165393 Oct 29 06:10 1446095400.V902I67a5b3eM290158.harvester
-rw------- 1 user user 105054 Oct 29 06:10 1446095430.V902I67a5b3fM265065.harvester
-rw------- 1 user user   1615 Oct 29 06:24 1446096244.V902I67a5b40M55701.harvester
-rw------- 1 user user   1620 Oct 29 06:24 1446096292.V902I67a5b41M337769.harvester
-rw------- 1 user user  10436 Oct 29 06:36 1446096973.V902I67a5b42M707215.harvester
-rw------- 1 user user   7150 Oct 29 06:36 1446097019.V902I67a5b43M415731.harvester
-rw------- 1 user user   4357 Oct 29 06:39 1446097194.V902I67a5b56M446687.harvester
-rw------- 1 user user   4283 Oct 29 06:39 1446097195.V902I67a5b57M957052.harvester
-rw------- 1 user user   4393 Oct 29 06:39 1446097197.V902I67a5b58M774506.harvester
-rw------- 1 user user   4264 Oct 29 06:39 1446097198.V902I67a5b59M532213.harvester
-rw------- 1 user user   4272 Oct 29 06:40 1446097201.V902I67a5b5aM534679.harvester
-rw------- 1 user user   4274 Oct 29 06:40 1446097228.V902I67a5b5dM363553.harvester
-rw------- 1 user user  20905 Oct 29 06:44 1446097455.V902I67a5b5eM918314.harvester

实际上,它只是列出了当前目录中的所有文件.我们可以以这些文件之一为例,检查其修改时间是否确实如ls命令所显示:

Actually, it just listed all files in the current directory. We can take one of these files as an example and check if its modification time is really as displayed by the ls command:

stat 1446070435.V902I67a5567M283852.harvester

  File: ‘1446070435.V902I67a5567M283852.harvester’
  Size: 9065        Blocks: 24         IO Block: 4096   regular file
Device: 902h/2306d  Inode: 108680551   Links: 1
Access: (0600/-rw-------)  Uid: ( 1001/   user)   Gid: ( 1027/   user)
Access: 2015-10-28 23:13:55.281515368 +0100
Modify: 2015-10-28 23:13:55.281515368 +0100
Change: 2015-10-28 23:13:55.313515539 +0100

我们可以看到,该文件肯定比1小时前的更早进行了最后一次修改!我也尝试了find -mmin 60find -mmin +60,但是它也不起作用.

As we can see, this file was definitely last modified earlier than 1 hour ago! I also tried find -mmin 60 or find -mmin +60, but it did not work either.

为什么会发生这种情况以及如何正确使用find命令?

Why is this happening and how to use the find command correctly?

推荐答案

如果目录中没有最近一个小时没有修改的文件,我可以重现您的问题.在这种情况下,find . -mmin -60不返回任何内容.但是,命令find . -mmin -60 |xargs ls -l返回目录中的每个文件,这与ls -l不带参数运行时发生的情况一致.

I can reproduce your problem if there are no files in the directory that were modified in the last hour. In that case, find . -mmin -60 returns nothing. The command find . -mmin -60 |xargs ls -l, however, returns every file in the directory which is consistent with what happens when ls -l is run without an argument.

要确保仅在找到文件时运行ls -l,请尝试:

To make sure that ls -l is only run when a file is found, try:

find . -mmin -60 -type f -exec ls -l {} +

这篇关于如何查找最近x分钟内修改过的文件(查找-mmin不能按预期工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆