检查两台服务器上的文件修改

如果我在服务器A和服务器B上有一个文件,即/etc/file.cfg。我们如何检查服务器A上的文件是否比服务器B上的文件新?

如果它在本地机器上,那么我们将使用file1 -nt file2。

如果你有SSH访问,你可以尝试下面的东西。 请注意,没有错误检查,例如,如果远程文件不存在,这将失败。

REMOTE_TIME_1=$(ssh user@remote "ls -l --time-style=+%s remote_file | cut -d ' ' -f 6") REMOTE_TIME_2=$(ssh user@remote "ls -l --time-style=+%s remote_file | cut -d ' ' -f 6") if (( REMOTE_TIME_1 > REMOTE_TIME_2 )); then echo first file is older else echo second file is older fi 

请注意,如果您不想每次input密码,都可以使用ssh-copy-id命令将您的公钥复制到远程服务器的authorized_keys文件中。