64

我正在尝试查找在过去 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 60or find -mmin +60,但也没有用。

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

4

8 回答 8

69

如果目录中没有在过去一小时内修改过的文件,我可以重现您的问题。在这种情况下,find . -mmin -60什么也不返回。然而, command返回目录中的每个文件,这与在没有参数的情况下运行find . -mmin -60 |xargs ls -l时发生的情况一致。ls -l

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

find . -mmin -60 -type f -exec ls -l {} +
于 2015-10-29T06:40:32.517 回答
32

问题是

find . -mmin -60

输出:

.
./file1
./file2

注意带一个点的线?
这使得lslist 整个目录与执行时完全相同ls -l .

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

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

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

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

要不就:

find . -mmin -60 -type f -ls

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

find . -mmin -60 -ls
于 2015-10-29T09:33:57.770 回答
24

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

$ find /target_directory -type f -mmin -60

要查找最近修改的文件,按更新时间的倒序排序(即最近更新的文件在前):

$ find /etc -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort -r
于 2018-02-01T09:58:41.790 回答
7

查找手册:

   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
于 2015-10-29T06:47:20.243 回答
5

我正在解决同样的需求,我相信你的时间表是不正确的。

试试这些:

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

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

于 2017-03-01T13:39:06.483 回答
0

其实这里的问题不止一个。主要的是xargs默认情况下执行您指定的命令,即使没有传递任何参数。要改变这一点,您可以使用 GNU 扩展来xargs

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

简单的例子:

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

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

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

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

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

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

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

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

find . -type f -mmin -60 -print0 | xargs -r0 ls -l
于 2016-01-29T18:03:43.090 回答
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
}
于 2015-10-29T06:47:44.560 回答
-3

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

find -type f -mtime -60
于 2015-10-29T06:44:26.810 回答