Reputation: 16482
I'm downloading a file and then unzipping it from a Bash script file.
#!/bin/sh
wget -N http://example.com/file.zip
unzip -o file.zip
Is there a way to check if wget
actually downloaded a new file? For instance, if the remote version of file.zip
is the same as the local version it will not retrieve the file. I only want to unzip the file if wget
actually retrieves a new file.
Upvotes: 5
Views: 5609
Reputation: 11
It's an old question but don't work anymore. I don't have any return when I set the quiet option of wget in the 2 case but we can have the HTML Code with the -S option of wget.
-200 if the file is downloaded
-304 if is the same file
-others... for all "bad" situations
a solution without change the @anubhava method's :
out=$(wget -SN 'http://example.com/file.zip' 2>&1 | grep "HTTP/" | awk '{print $2}')
[[ $out -eq 200 ]] && unzip file.zip
Upvotes: 1
Reputation: 181
Don't use the Last-Modified header, that's dependent on the server. Anubhava@'s also works but this is less overhead and slightly more portable between Bourne shell variations:
This gets you what you need:
wget -N http://example.com/file.zip 2>&1 | grep "not retrieving" 2>&1 > /dev/null || unzip file.zip
It's essentially saying this, with more detail added for readability:
out=$(wget -N http://example.com/file.zip 2>&1)
if [ $(echo $(out) | grep "not retrieving") ]; then
echo "No new file; not unzipping"
else
unzip file.zip
fi
Upvotes: 2
Reputation: 785058
You should check return value and output from wget
to figure out whether file has been downloaded:
out=$(wget -qN 'http://example.com/file.zip' 2>&1)
[[ $? -eq 0 && $out ]] && unzip file.zip
If file.zip
is already there with same timestamp then wget
will not download it and nothing will be written to stdout/stderr making out
variable empty.
Upvotes: 4
Reputation: 69198
You can use
curl -I http://example.com/file.zip
and check Last-Modified:
value.
You may also use wget --timestamping
but requesting HEAD info you have more control.
Upvotes: -1