3

I downloading files using wget like this

wget http://www.example.com/mysql.zip -O mysql.zip

Then i have do stuff with that file.

But sometimes if there is error in url or somewhere then corrupted file mysql.zip

is placed in there and my script don't download the file as its already there and script fails.

Is there any way if mysql.zip is only placed if file download is complete.

There are two cases when that can happen

  1. Url does not exist
  2. User manually cancels the download

IN both above cases i don't want the file to be there

2
  • Download to a temporary file and only mv to the final location if wget completes succesfully. Commented Jul 2, 2015 at 2:39
  • @EtanReisner i am doing that in shell script , how can check if wget downloaded successfuly Commented Jul 2, 2015 at 2:40

2 Answers 2

6

wget http://www.example.com/mysql.zip -O mysql.zip || rm -f mysql.zip

0
5

What about this bash script:

#!/bin/bash
if wget http://www.example.com/mysql.zip -O mysql.zip
then
    # Do something with file
else
    rm mysql.zip
fi
1
  • it can be done a lot more easier with a logical operator like @Denio Mariz does ;) But great answer anyways
    – Fipsi
    Commented Aug 4, 2019 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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