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.
Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.Sign up to join this community
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.