Bash - 如何比较两个远程文件的最后修改日期?

Bash - How to compare two files last modified date, when they are remote?

我的问题与 this 类似。除了我要检查日期的文件位于我 ssh onto/scp 关闭的远程服务器上。 基本上,我希望能够比较文件在我的本地计算机和远程服务器上的最后修改日期,以及较新的副本到另一台计算机的日期。我知道如何进行所有复制和其他操作,我只想弄清楚如何比较日期。

试试这个:

remote=$(ssh user@server "stat -c %Y /path/to/remote_file")
[[ -z "$remote" ]] && exit 1 # stop on error ($remote is empty)
local=$(stat -c %Y /path/to/local_file)

if [[ $remote -gt $local ]]; then
  echo remote file is newer
else
  echo local file is newer
fi