58

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

观察:

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

8 回答 8

156

如果您使用 GNU find,则从 4.3.3 版开始,您可以执行以下操作:

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

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

您可以将cin更改-newerctaBcm查看 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.
于 2014-05-07T04:15:38.383 回答
52

尝试以下命令:

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

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

于 2014-09-11T09:24:53.307 回答
15

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

我正在使用 find (GNU findutils) 4.5.11

$ find search/path/ -newermt 20130801 \! -newermt 20130831
于 2016-05-13T21:33:00.180 回答
10

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

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

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")
于 2013-08-20T15:53:30.963 回答
6

用于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
于 2013-08-20T15:53:20.790 回答
2

脚本旧文件

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

该脚本oldfiles在此存储库中

要“创建”一个新的 find 命令,您可以使用选项 (dry-run) 运行它,它会向您打印您需要使用-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

于 2017-07-07T05:58:02.427 回答
2

您可以使用以下命令列出 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"
于 2020-05-27T07:23:44.160 回答
0

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

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
于 2020-05-06T17:01:04.860 回答