简体   繁体   English

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

[英]How to find files modified in last x minutes (find -mmin does not work as expected)

I'm trying to find files modified in last x minutes, for example in the last hour. 我正在尝试查找最近x分钟内修改的文件,例如在最后一小时内。 Many forums and tutorials on the net suggest to use the find command with the -mmin option, like this: 网上的许多论坛和教程建议使用带有-mmin选项的find命令,如下所示:

find . -mmin -60 |xargs ls -l

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: 从下面的清单中可以看出,它还显示了早于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

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: 我们可以将其中一个文件作为示例,并检查其修改时间是否真的与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

As we can see, this file was definitely last modified earlier than 1 hour ago! 我们可以看到,这个文件肯定比1小时前更早修改! I also tried find -mmin 60 or find -mmin +60 , but it did not work either. 我也试过find -mmin 60或者find -mmin +60 ,但它也find -mmin +60

Why is this happening and how to use the find command correctly? 为什么会发生这种情况以及如何正确使用find命令?

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 在那种情况下, find . -mmin -60 find . -mmin -60 returns nothing. find . -mmin -60什么都不返回。 The command find . -mmin -60 |xargs ls -l 命令find . -mmin -60 |xargs ls -l 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. find . -mmin -60 |xargs ls -l返回目录中的每个文件,这与在没有参数的情况下运行ls -l时发生的情况一致。

To make sure that ls -l is only run when a file is found, try: 要确保仅在找到文件时运行ls -l ,请尝试:

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

The problem is that 问题是

find . -mmin -60

outputs: 输出:

.
./file1
./file2

Note the line with one dot? 注意带有一个点的线?
That makes ls list the whole directory exactly the same as when ls -l . 这使得ls列出整个目录与ls -l .完全相同ls -l . is executed. 被执行。

One solution is to list only files (not directories): 一种解决方案是仅列出文件(而不是目录):

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

But it is better to use directly the option -exec of find: 但最好直接使用find的-exec选项:

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

Or just: 要不就:

find . -mmin -60 -type f -ls

Which, by the way is safe even including directories: 哪个,顺便说一下,即使包括目录也是安全的:

find . -mmin -60 -ls

To search for files in /target_directory and all its sub-directories, that have been modified in the last 60 minutes: 要搜索/ target_directory及其所有子目录中的文件,这些文件在过去60分钟内已被修改:

$ find /target_directory -type f -mmin -60

To find the most recently modified files, sorted in the reverse order of update time (ie, the most recently updated files first): 要查找最近修改的文件,请按照更新时间的相反顺序排序(即,最近更新的文件首先):

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

Manual of find: 查找手册:

   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.

Example: 例:

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

I am working through the same need and I believe your timeframe is incorrect. 我正在努力满足同样的需求,我相信你的时间表不正确。

Try these: 试试这些:

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

You should be using 24 hours as your base. 您应该使用24小时作为基础。 The number after -mtime should be relative to 24 hours. -mtime之后的数字应相对于24小时。 Thus -.5 is the equivalent of 12 hours, because 12 hours is half of 24 hours. 因此-.5相当于12小时,因为12小时是24小时的一半。

This may work for you. 这可能对你有用。 I used it for cleaning folders during deployments for deleting old deployment files. 我在部署期间用它来清理文件夹以删除旧的部署文件。

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
}

Actually, there's more than one issue here. 实际上,这里有不止一个问题。 The main one is that xargs by default executes the command you specified, even when no arguments have been passed. 主要的一点是默认情况下xargs执行您指定的命令,即使没有传递参数也是如此。 To change that you might use a GNU extension to xargs : 要更改它,您可以使用GNU扩展到xargs

--no-run-if-empty --no-运行,如果空
-r -r
If the standard input does not contain any nonblanks, do not run the command. 如果标准输入不包含任何非空白,请不要运行该命令。 Normally, the command is run once even if there is no input. 通常,即使没有输入,命令也会运行一次。 This option is a GNU extension. 此选项是GNU扩展。

Simple example: 简单的例子:

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

But this might match to all subdirectories, including . 但这可能与所有子目录相匹配,包括. (the current directory), and ls will list each of them individually. (当前目录), ls将单独列出每个目录。 So the output will be a mess. 所以输出会很乱。 Solution: pass -d to ls , which prohibits listing the directory contents: 解决方案:将-d传递给ls ,禁止列出目录内容:

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

Now you don't like . 现在你不喜欢. (the current directory) in your list? (列表中的当前目录)? Solution: exclude the first directory level ( 0 ) from find output: 解决方案:从find输出中排除第一个目录级别( 0 ):

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

Now you'd need only the files in your list? 现在你只需要列表中的文件了吗? Solution: exclude the directories: 解决方案:排除目录:

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

Now you have some files with names containing white space, quote marks, or backslashes? 现在你有一些名字包含空格,引号或反斜杠的文件? Solution: use null-terminated output (find) and input (xargs) (these are also GNU extensions, afaik): 解决方案:使用以null结尾的输出(find)和输入(xargs)(这些也是GNU扩展,afaik):

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

先生,这个命令可能对你有所帮助

find -type f -mtime -60

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM