bash - 用 bash 测试文件日期

标签 bash file datetime creation

我正在尝试在 if 语句中使用 bash 测试文件的创建时间(以秒为单位)。我需要创建日期,而不是修改。

如果不使用 findgrep 这样的命令,您是否知道如何做到这一点?

最佳答案

恐怕我无法回答创建时间的问题,但对于上次修改时间,您可以使用以下方法获取纪元日期,以为单位,因为文件名 上次修改:

date --utc --reference=filename +%s

所以你可以这样:

modsecs=$(date --utc --reference=filename +%s)
nowsecs=$(date +%s)
delta=$(($nowsecs-$modsecs))
echo "File $filename was modified $delta secs ago"

if [ $delta -lt 120 ]; then
  # do something
fi

等..

更新 一种更优雅的方式(同样,仅修改时间):how do I check in bash whether a file was created more than x time ago?

关于bash - 用 bash 测试文件日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1819187/

相关文章:

linux - set -e 覆盖bash脚本中的参数变量($@)

linux - 如何使 bash 命令回显线变为绿色

python - rc.local shell 脚本启动时缺少文件

比较字符串和 strftime 值时,运算符中的 Python 无法按预期工作

JavaScript:意外结果获取该月剩余天数

linux - 动态获取由 docker run 命令创建的正在运行的容器 ID/名称

python - 将文件作为函数参数传递

java - 如何在 Windows 中检测文件重命名/move

c++ - "Attribute is protected within this context"具有继承和 .h 和 .cpp 文件

ruby-on-rails - 仅使用 Ruby 内置函数将秒数转换为小时数 - 除了天数