查找在日期范围之间创建的文件

san*_*998 32 linux aix find

我在工作中通过telnet使用AIX,我想知道如何在日期范围之间查找特定文件夹中的文件.例如:我想查找文件夹X中在01年8月13日到31日 - 8月13日之间创建的所有文件.

观察:

  • touch一旦我在服务器上的用户角色不允许我创建文件,技巧(你创建两个空文件以使用-newer选项)对我不起作用.
  • 我需要在特定日期之间找到,而不是在几天之间(例如:超过30天前创建的文件等等)

cod*_*ard 99

如果你使用GNU find,从版本4.3.3开始,你可以:

find -newerct "1 Aug 2013" ! -newerct "1 Sep 2013" -ls
Run Code Online (Sandbox Code Playgroud)

它将接受GNU接受的任何日期字符串date -d.

您可以将改变c-newerct任何的a,B,c,或m为寻找的atime /生/的ctime /修改时间.

另一个例子 - 2017年11月6日17:30至22:00之间修改的列表文件:

find -newermt "2017-11-06 17:30:00" ! -newermt "2017-11-06 22:00:00" -ls
Run Code Online (Sandbox Code Playgroud)

详细信息来自man find:

   -newerXY reference
          Compares the timestamp of the current file with reference.  The reference argument is normally the name of a file (and one of its timestamps  is  used
          for  the  comparison)  but  it may also be a string describing an absolute time.  X and Y are placeholders for other letters, and these letters select
          which time belonging to how reference is used for the comparison.

          a   The access time of the file reference
          B   The birth time of the file reference
          c   The inode status change time of reference
          m   The modification time of the file reference
          t   reference is interpreted directly as a time

          Some combinations are invalid; for example, it is invalid for X to be t.  Some combinations are not implemented on all systems; for example B  is  not
          supported on all systems.  If an invalid or unsupported combination of XY is specified, a fatal error results.  Time specifications are interpreted as
          for the argument to the -d option of GNU date.  If you try to use the birth time of a reference file, and the birth time cannot be determined, a fatal
          error  message  results.   If  you  specify a test which refers to the birth time of files being examined, this test will fail for any files where the
          birth time is unknown.
Run Code Online (Sandbox Code Playgroud)

  • @Koshmaar如果文件太多,将无法使用,因为exec命令将开始被拆分。在这种情况下,您需要使用类似“查找... -printf'%C @%p \ n'| 排序` (3认同)
  • @Koshmaar根据情况,您可以使用诸如`find ... -exec ls -dilst {} +`之类的东西。ls的t选项将使它按时间排序。有关其他排序选项,请参见ls手册页。 (2认同)

小智 41

试试这个:

find /var/tmp -mtime +2 -a -mtime -8 -ls

查找超过2天但不超过8天的文件.

  • `-a` 可以省略。 (2认同)

Jas*_*ski 12

这里有一些好的解决方案.想要分享我的,简而言之.

我正在使用find(GNU findutils)4.5.11

$ find search/path/ -newermt 20130801 \! -newermt 20130831
Run Code Online (Sandbox Code Playgroud)

  • 喜欢魅力.谢谢杰森.易于阅读,易于编写,并具有所需的功能.不知道`-newerXY'技巧,我发现它谢谢你的答案. (2认同)

小智 6

您可以使用以下内容找到您需要的内容.

查找早于特定日期/时间的文件:

find ~/ -mtime $(echo $(date +%s) - $(date +%s -d"Dec 31, 2009 23:59:59") | bc -l | awk '{print $1 / 86400}' | bc -l)
Run Code Online (Sandbox Code Playgroud)

或者您可以在两个日期之间找到文件.第一次约会更近,最后一次约会,更早.你可以下到第二个,你不必使用mtime.你可以随心所欲地使用它.

find . -mtime $(date +%s -d"Aug 10, 2013 23:59:59") -mtime $(date +%s -d"Aug 1, 2013 23:59:59")
Run Code Online (Sandbox Code Playgroud)

  • 第二个命令不正确.`date +%s`从1970年开始返回*秒*,但是`find -mtime`需要*天前*天*:`-mtime n文件的数据最后一次修改n*24小时前.我会推荐@codebeard的[回答] (http://stackoverflow.com/a/23508622/2267932)哪个更方便. (7认同)
  • 第二个命令不起作用;(`$ ls -cl test_file -rw ------- 1 vv 0 Nov 16 22:00 test_file $ find.-mindepth 1 -maxdepth 1 -mtime $(date +%s - d"2015年11月16日22:00:00")-mtime $(日期+%s -d"2015年11月16日22:01:00")$` (5认同)

cho*_*oba 5

使用stat来获取创建时间.您可以按YYYY-MM-DD HH:MM:SS字典顺序比较格式的时间.

这项工作在Linux上具有修改时间,创建时间不受支持.在AIX上,-c可能不支持该选项,但无论如何都应该能够获取信息,grep如果没有其他工作则可以使用.

#! /bin/bash
from='2013-08-01 00:00:00.0000000000' # 01-Aug-13
to='2013-08-31 23:59:59.9999999999'   # 31-Aug-13

for file in * ; do
    modified=$( stat -c%y "$file" )
    if [[ $from < $modified && $modified < $to ]] ; then
        echo "$file"
    fi
done
Run Code Online (Sandbox Code Playgroud)

  • @ user2576376:如果您不熟悉目标系统,请不要在telnet中运行任何命令.找一个知道该怎么做的人. (3认同)

小智 5

您可以使用以下命令列出 2 个特定日期之间的文件:

在当前 ( ) 目录中搜索.

find . -type f -newermt "2019-01-01" ! -newermt "2019-05-01"
Run Code Online (Sandbox Code Playgroud)

在目录上搜索/var/any/directory/

find /var/any/directory/ -type f -newermt "2019-01-01" ! -newermt "2019-05-01"
Run Code Online (Sandbox Code Playgroud)