【问题标题】:How can I find a file that was created within X minutes of a another file?如何找到在另一个文件的 X 分钟内创建的文件?
【发布时间】:2011-06-06 22:16:54
【问题描述】:

我正在搜索一个非空白文件(标题值除外)并希望找到在该文件的 1 分钟(或 X 分钟)内创建的所有其他文件。

到目前为止我有:

tail -n +2 `ls -rt *.csv | tail -n 1` > watch_me.txt

这将修剪最后创建的 csv 文件的第一行。如果 watch_me.txt 不是空白,那么我想查找在我正在搜索的 csv 文件的 1 分钟(或 X 分钟)内创建的所有文件。

【问题讨论】:

  • 大概是某种基于 Linux 的 shell?请您添加一些更具体的标签吗?
  • 会的!我在linux上使用bash。谢谢

标签: linux bash shell


【解决方案1】:

此命令将查找当前目录中在文件 watch_me.txt 修改时间后 60 秒(+ 或 -)内修改的所有文件

ts=$(stat --printf="%Y" watch_me.txt);
tsmin=$(date -d @$((ts-60)));
tsmax=$(date -d @$((ts+60))); 
find . -maxdepth 1 -newermt "$tsmin" -not -newermt "$tsmax" 

【讨论】:

  • 在我使用的系统上 find 不允许 -newermt 标志。我在看看我是否可以稍微修改你的脚本以获取我想要的内容。
  • 可能(甚至更简单)你可以这样做for f in $(find . -type f) ; do <here get stat of file and compare with tsmin and tsmax> ; done
【解决方案2】:

会很简单

ls -alt

即按文件修改时间排序就够了吗? 然后 grep 使用 -A-B 开关提取 匹配文件之后或之前的匹配行。

【讨论】:

    猜你喜欢
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2010-10-22
    相关资源
    最近更新 更多