Grant Grant - 1 month ago 5x
Linux Question

wget Downloading and replacing file only if target is newer than source

This is what I'm trying to achieve :

User uploads file1.jpg to Server A

Using wget Server B only downloads file1.jpg from Server A if the file is newer than the one that already exists on Server B and then replaces the file on Server B with the new one.

I know I can use :

wget -N

To check that the target file is newer than the source but I'm a little confused as to how I format the command to let it know what and where the actual source file is?

Is it something like? :

wget -N /serverb/files/file1.jpg



You can use -P option to specify the directory where the file(s) will be downloaded:

$ wget -N -P /serverb/files/

You are also talking about downloading and replacing the file. Be aware, that wget overwrites the file, so it is "broken" while being downloaded. I don't think you can do atomic replacement of the file using only wget. You need a small script that uses temporary files and move to atomically replace the file in Server B.