Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
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

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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