如何查找最近x分钟内修改的文件(查找-mmin不能按预期工作)

时间:2015-10-29 06:30:53

标签: linux bash unix gnu-findutils

我试图查找在过去x分钟内修改的文件,例如在最后一小时内。网上的许多论坛和教程建议使用带有-mmin选项的find命令,如下所示:

find . -mmin -60 |xargs ls -l

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

-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命令所示:

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,但它也没有用。

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

8 个答案:

答案 0 :(得分:45)

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

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

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

答案 1 :(得分:12)

问题在于

.
./file1
./file2

输出:

ls

注意带有一个点的线?
这使得ls -l .列出整个目录与执行find . -mmin -60 -type f | xargs ls -l 时完全相同。

一种解决方案是仅列出文件(而不是目录):

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

但最好直接使用find的选项-exec:

find . -mmin -60 -type f -ls

或者只是:

find . -mmin -60 -ls

顺便说一下,即使包括目录也是安全的:

tr

答案 2 :(得分:8)

搜索/ target_directory及其所有子目录中的文件,这些文件在过去60分钟内已被修改:

$ find /target_directory -type f -mmin -60

要查找最近修改过的文件,请按照更新时间的相反顺序排序(即,最近更新的文件首先):

$ find /etc -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort -r

答案 3 :(得分:3)

查找手册:

   Numeric arguments can be specified as

   +n     for greater than n,

   -n     for less than n,

   n      for exactly n.

   -amin n
          File was last accessed n minutes ago.

   -anewer file
          File was last accessed more recently than file was modified.  If file is a symbolic link and the -H option or the -L option is in effect, the access time of the file it points  to  is  always
          used.

   -atime n
          File  was  last  accessed  n*24 hours ago.  When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to
          have been accessed at least two days ago.

   -cmin n
          File's status was last changed n minutes ago.

   -cnewer file
          File's status was last changed more recently than file was modified.  If file is a symbolic link and the -H option or the -L option is in effect, the status-change time of the file it  points
          to is always used.

   -ctime n
          File's status was last changed n*24 hours ago.  See the comments for -atime to understand how rounding affects the interpretation of file status change times.

示例:

find /dir -cmin -60 # creation time
find /dir -mmin -60 # modification time
find /dir -amin -60 # access time

答案 4 :(得分:3)

我正在努力解决同样的问题,我相信你的时间表不正确。

试试这些:

  • 15分钟变化:找到。 -mtime -.01
  • 1小时改变:找到。 -mtime -.04
  • 12小时变化:找到。 -mtime -.5

您应该使用24小时作为基础。 -mtime之后的数字应相对于24小时。因此-.5相当于12小时,因为12小时是24小时的一半。

答案 5 :(得分:0)

这可能对你有用。我在部署期间用它来清理文件夹以删除旧的部署文件。

clean_anyfolder() {
    local temp2="$1/**"; //PATH
    temp3=( $(ls -d $temp2 -t | grep "`date | awk '{print $2" "$3}'`") )
    j=0;
    while [ $j -lt ${#temp3[@]} ]
    do
            echo "to be removed ${temp3[$j]}"
            delete_file_or_folder ${temp3[$j]} 0 //DELETE HERE
        fi
        j=`expr $j + 1`
    done
}

答案 6 :(得分:0)

实际上,这里有不止一个问题。主要的一点是xargs默认执行您指定的命令,即使没有传递参数也是如此。要更改它,您可以使用GNU扩展名xargs

  

- 无 - 运行 - 如果空
  -r
  如果标准输入不包含任何非空白,请不要运行该命令。通常,即使没有输入,命令也会运行一次。此选项是GNU扩展。

简单示例:

find . -mmin -60 | xargs -r ls -l

但这可能与所有子目录匹配,包括.(当前目录),ls将分别列出每个子目录。所以输出会很乱。解决方案:将-d传递给ls,禁止列出目录内容:

find . -mmin -60 | xargs -r ls -ld

现在您不喜欢列表中的.(当前目录)?解决方案:从查找输出中排除第一个目录级别(0):

find . -mindepth 1 -mmin -60 | xargs -r ls -ld

现在您只需要列表中的文件?解决方案:排除目录:

find . -type f -mmin -60 | xargs -r ls -l

现在您有一些名称包含空格,引号或反斜杠的文件?解决方案:使用以null结尾的输出(find)和输入(xargs)(这些也是GNU扩展,afaik):

find . -type f -mmin -60 -print0 | xargs -r0 ls -l

答案 7 :(得分:-2)

这个命令可能会帮助你先生

find -type f -mtime -60
相关问题