Consider using curl
instead of wget
:
curl -o "$file" -z "$file" "$uri"
man curl
says:
-z
/--time-cond
<date expression>
(HTTP/FTP) Request a file that has been modified later than the given time and date, or one
that has been modified before that time. The date expression can be all sorts of date
strings or if it doesn't match any internal ones, it tries to get the time from a given file name instead.
If $file
doesn't necessarily pre-exist, you'll need to make the use of the -z
flag conditional, using test -e "$file"
:
if test -e "$file"
then zflag="-z '$file'"
else zflag=
fi
curl -o "$file" $zflag "$uri"
(Note that we don't quote the expansion of $zflag
here, as we want it to undergo splitting to 0 or 2 tokens).
If your shell supports arrays (e.g. Bash), then we have a safer and cleaner version:
if test -e "$file"
then zflag=(-z "$file")
else zflag=()
fi
curl -o "$file" "${zflag[@]}" "$uri"
Why not download the file and then rename it? – Julian Knight – 2015-04-30T22:50:57.490
.. because the tool still needs to be able to check if the HTTP resource changed since the last download? This will be hard if the file has been renamed and thus does not exist anymore at the place the tool expects it. – cweiske – 2015-05-01T07:33:14.367
Sorry, I rushed that comment, see my answer. – Julian Knight – 2015-05-01T08:05:40.483