【问题标题】:How to compare mtime of symbolic links in bash?如何比较bash中符号链接的mtime?
【发布时间】:2018-03-15 18:23:58
【问题描述】:

在 bash 中,可以测试 file1 是否比 file2 旧:

  if [[ file1 -ot file2 ]]; then
    ...
  fi

如果file1file2(或两者)是符号链接,我希望通过符号链接本身的 mtime 比较文件,而不是它指向的文件(即我希望 bash 不遵循符号链接)。

甚至可以不使用外部程序吗?无论如何,最好的方法是什么。

【问题讨论】:

  • 你必须使用像stat这样的外部程序。

标签: bash symlink filemtime


【解决方案1】:
#Compares modification dates on files (or links for that matter)
fname1=f1.txt
fname2=f2.txt

file1=$(date +%s -d "$(stat $fname1 | grep Modify | awk '{print $2" "$3}')")
file2=$(date +%s -d "$(stat $fname2 | grep Modify | awk '{print $2" "$3}')")

if [ $file1 -gt $file2 ];then
    echo $fname1 is newer than $fname1
else
    echo $fname1 is older than $fname2
fi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 2016-03-17
    • 2017-04-10
    • 1970-01-01
    • 2018-02-24
    相关资源
    最近更新 更多