【问题标题】:Find files in created between a date range查找在日期范围之间创建的文件
【发布时间】:2013-08-22 18:07:54
【问题描述】:

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

观察:

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

【问题讨论】:

    标签: linux find aix


    【解决方案1】:

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

    find -newerct "1 Aug 2013" ! -newerct "1 Sep 2013" -ls
    

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

    您可以将-newerct 中的c 更改为aBcm 中的任何一个,以查看atime/birth/ctime/mtime。

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

    find -newermt "2017-11-06 17:30:00" ! -newermt "2017-11-06 22:00:00" -ls
    

    来自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.
    

    【讨论】:

    • 有人知道如何按时间对结果列表进行排序吗?
    • @Koshmaar 根据具体情况,你可以使用类似find ... -exec ls -dilst {} + ls 的 t 选项将使其按时间排序;有关其他排序选项,请参见 ls 手册页。
    • @Koshmaar 如果文件太多,这将不起作用,因为 exec 命令将开始被拆分。在这种情况下,您需要使用类似find ... -printf '%C@ %p\n' | sort
    • 尝试执行 find 。 -newermt $(date -d "-2 days" +"%Y-%m-%d %k:%M:%S") ! -newermt $(date -d "-1 days" +"%Y-%m-%d %k:%M:%S") -ls 返回错误查找:路径必须在表达式之前:`14:33:20'为什么?
    • 我认为应该使用-Btime-newerBt,严格用于创建时间,而c 用于文件更改
    【解决方案2】:

    试试下面的命令:

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

    这将允许您在/var/tmp 文件夹中查找早于2 天但不早于8 天的文件。

    【讨论】:

    • 对于-a(和)标志,太棒了
    • -a 可以省略。
    • 这对我有用。对于+- 符号,这里是find 手册页中的插图:+n 表示大于n,-n 表示小于n,n 表示正好n。
    【解决方案3】:

    这里有一些很好的解决方案。想分享我的,因为它简短而简单。

    我正在使用 find (GNU findutils) 4.5.11

    $ find search/path/ -newermt 20130801 \! -newermt 20130831
    

    【讨论】:

    • 就像一个魅力。谢谢杰森。易于阅读,易于编写并且具有所需的功能。不知道-newerXY 的把戏,我发现了,谢谢你的回答。
    • 答案与@codebeard 2 年前的答案相同,但此处没有描述
    • 我想你是想写\! -newermt 20130901
    【解决方案4】:

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

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

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

    或者您可以查找两个日期之间的文件。第一个日期较近,最后一个日期较旧。可以下到秒,不用mtime。你可以使用任何你需要的东西。

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

    【讨论】:

    • 我使用了第二个选项,但我得到了这个错误:日期/时间规范中的字符无效。用法:date [-u] [+Field Descriptors] 日期/时间规范中的无效字符。用法:date [-u] [+Field Descriptors] find:为-mtime指定一个十进制整数用法:find [-H | -L] 路径列表 [谓词列表]
    • AIX... 这是关于 AIX 中 DATE 命令的 IBM 参考资料。 pic.dhe.ibm.com/infocenter/aix/v7r1/… 如果您知道如何在 AIX 中打印日期,您可以将它与第二个命令一起使用。抱歉,我不是 AIX 专家。
    • 这对我有用find . -maxdepth 1 -mmin -$(echo $(date +%s) - $(date +%s -d"Sep 9, 2015 15:03:00") | bc -l | awk '{print $1 / 60}' ) -mmin +$(echo $(date +%s) - $(date +%s -d"Sep 9, 2015 16:21:00") | bc -l | awk '{print $1 / 60}' ) -exec ls -ld --time-style=full-iso {} \;
    • 第二个命令不起作用 ;( $ ls -cl test_file -rw------- 1 v v 0 Nov 16 22:00 test_file $ find . -mindepth 1 -maxdepth 1 -mtime $(date +%s -d"Nov 16, 2015 22:00:00") -mtime $(date +%s -d"Nov 16, 2015 22:01:00") $
    • 第二个命令不正确。 date +%s 返回自 1970 年以来的 秒数,但 find -mtime 预计 今天前几天-mtime n File's data was last modified n*24 hours ago. 我会推荐 @codebeard 的 answer,这样更方便。
    【解决方案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
    

    【讨论】:

    • 如果我只是将它复制并粘贴到终端并执行它会起作用吗?我无法使用我的角色创建任何文件以将其用作脚本
    • @user2576376:如果你的 shell 是 bash,它应该是。
    • 是什么意思?对不起,对linux不熟悉...我正在通过telnet(命令提示符)在windows上执行这些comamnds,有帮助吗?
    • @user2576376:如果您对目标系统不熟悉,请不要在 telnet 中运行任何命令。找一个知道该怎么做的人。
    【解决方案6】:

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

    搜索当前 (.) 目录:

    find . -type f -newermt "2019-01-01" ! -newermt "2019-05-01"
    

    搜索/var/any/directory/目录:

    find /var/any/directory/ -type f -newermt "2019-01-01" ! -newermt "2019-05-01"
    

    【讨论】:

    • 答案与@codebeard 6 年前的答案相同,除了这里没有描述
    【解决方案7】:

    脚本旧文件

    我试图以更完整的方式回答这个问题,最后我创建了一个完整的脚本,其中包含帮助您理解find 命令的选项。

    脚本oldfiles在这个repository

    要“创建”一个新的查找命令,您可以使用选项 -n(试运行)运行它,它会向您打印您需要使用的正确的 find 命令。

    当然,如果你省略-n,它只会运行,不需要重新输入find 命令。

    用法:

    oldfiles [-v...] ([-h|-V|-n] | {[(-a|-u) | (-m|-t) | -c] (-i | -d | -o| -y | -g) N (-\> | -\< | -\=) [-p "pat"]})
    
    • 选项分为以下几组:
      • 帮助和信息:

    -h, --help :显示此帮助。
    -V, --version : 显示版本。
    -v, --verbose : 打开详细模式(累积)。
    -n, --dry-run :不运行,只说明如何创建“查找”命令

    • 时间类型(访问/使用、修改时间或更改状态):

    -a 或 -u :访问(使用)时间
    -m 或 -t :修改时间(默认)
    -c : inode 状态变化

    • 时间范围(其中 N 为正整数):

    -i N : 分钟(默认,N 等于 1 分钟)
    -d N : 天
    -o N : 月
    -y N : 年
    -g N : N 是一个日期(例如:“2017-07-06 22:17:15”)

    • 测试:

    -p "pat" : 匹配的可选模式(例如:-p "*.c" 查找 c 文件)(默认 -p "*")
    -\> :文件比给定范围新,即,在它之后修改的时间。
    - -= : 正好是 N(分、日、月、年)的文件。

    示例:

    • 查找更新时间超过 10 分钟(访问时间)的 C 源文件(详细程度为 3):

      oldfiles -a -i 10 -p"*.c" -\> -nvvv
      Starting oldfiles script, by beco, version 20170706.202054...
      oldfiles -vvv -a -i 10 -p "*.c" -\> -n
      Looking for "*.c" files with (a)ccess time newer than 10 minute(s)
      find . -name "*.c" -type f -amin -10 -exec ls -ltu --time-style=long-iso {} +
      Dry-run
      
    • 查找超过一个月的 H 头文件(修改时间)(详细程度 2):

      oldfiles -m -o 1 -p"*.h" -\< -nvv
      Starting oldfiles script, by beco, version 20170706.202054...
      oldfiles -vv -m -o 1 -p "*.h" -\< -n
      find . -name "*.h" -type f -mtime +30 -exec ls -lt --time-style=long-iso {} +
      Dry-run
      
    • 在一天内查找所有 (*) 文件(2016 年 12 月 1 日;不冗长,试运行):

      oldfiles -mng "2016-12-01" -\=
      find . -name "*" -type f -newermt "2016-11-30 23:59:59" ! -newermt "2016-12-01 23:59:59" -exec ls -lt --time-style=long-iso {} +
      

    当然,删除-n 程序会自行运行find 命令,省去你的麻烦。

    我希望这可以帮助大家最终了解这个{a,c,t}{time,min} 选项。

    LS 输出:

    您还会注意到ls 选项ls OPT 会根据您选择的时间类型发生变化。

    oldfiles 脚本的克隆/下载链接:

    https://github.com/drbeco/oldfiles

    【讨论】:

      【解决方案8】:

      说明:使用带有-ctime(创建时间)标志的unix命令find

      find 实用程序递归地向下列出每个路径的目录树,根据树中的每个文件评估一个表达式(由“primaries”和“operands”组成)。

      解决方案:根据官方文档:

      -ctime n[smhdw]
                   If no units are specified, this primary evaluates to true if the difference
                   between the time of last change of file status information and the time find
                   was started, rounded up to the next full 24-hour period, is n 24-hour peri-
                   ods.
      
                   If units are specified, this primary evaluates to true if the difference
                   between the time of last change of file status information and the time find
                   was started is exactly n units.  Please refer to the -atime primary descrip-
                   tion for information on supported time units.
      

      公式

      find <path> -ctime +[number][timeMeasurement] -ctime -[number][timeMeasurment]
      

      示例

      1.查找 1 周前和 2 周前创建的所有内容。

      find / -ctime +1w -ctime -2w
      

      2.查找当前目录中1天前到3天前创建的所有javascript文件(.js)。

      find . -name "*\.js" -type f -ctime +1d -ctime -3d
      

      【讨论】:

        猜你喜欢
        • 2022-10-15
        • 2016-10-24
        • 1970-01-01
        • 2014-08-05
        • 2014-12-06
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多