1

If I have a file namely /etc/file.cfg on server A and server B. How can we check whether the file on Server A is newer than file on server B ?

If it was on a local machine then we would have used file1 -nt file2.

3
  • 1
    Do you have SSH access to the remote server? If so, you can script the transfer of the file to the local system and compare the file as you currently do locally. – Bryan Dec 22 '11 at 14:54
  • Yes. I have a public key authentication between the 2 nodes. But when we copy the file to local server then it will change its modification timestamp. – Iliyas Dec 22 '11 at 15:22
  • You could use rsync -t to perform the copy, but the answer from @Can below, looks like a good way forward. – Bryan Dec 22 '11 at 15:27
3

If you have ssh access you can try something like below. Note that there is no error checking, for instance if remote files do not exist this would fail.

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

Note that if you do not want to enter password everytime you can use the ssh-copy-id command to copy your public key to the remote server's authorized_keys files.

2
  • 1
    Caveat: This will be the time recorded by the file system (it's trivially easy to set the times on files to arbitrary values); even if it's not tampered with it depends on the clock being set correctly at the time of editing. These probably don't matter in this situation, but it's worth mentioning. – Chris S Dec 22 '11 at 15:21
  • @Chris Good point. You must be trusting remote systems. – Can Kavaklıoğlu Dec 22 '11 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.