Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I write wget "no such address" -o "test.html" it first creates the test.html and in case of failure, leaves it empty. However, when not using -o, it will wait to see if the download succeeds and only after that, it'll write the file.

I'd like the latter behavior to be applied to -o too, is it possible?

share|improve this question

wget returns a non-zero exit status when the URL is not found, so you can append a remove command on failure:

wget "url" -O file || rm -f file

Or create a temporary file and only move it where you want on success:

wget "url" -O /tmp/wget && mv /tmp/wget file

The second has the benefit of not deleting an existing file on failure, but be sure to use unique temporary names (see man tempfile) if you're running multiple instances in parallel.

share|improve this answer

The correct syntax is

wget "url" -O file

notice the UPPERCASE O. The -o options tells wget to write a log file, that's why it's always written even on failure.

share|improve this answer
    
At first I thought it was working, but then I found it didn't. try wget "host.does.not.exist"; -O "emptyFile" An error is returned, yet the emptyFile is created. – akurtser Jul 22 '10 at 9:38
1  
@akurtser you're right. I think there's no way to tell wget not to create the file. I found this thread: mail-archive.com/wget@sunsite.dk/msg08586.html in which they discuss the matter. The baseline is that you can have MULTIPLE downloads to the same file so it gets created because wget cannot be shure that ALL urls will fail. – Mr Shunz Jul 22 '10 at 11:41
    
Well thanks, it's a part of a bash script I'm writing, so I'll just try to first save it a temp file, which in case of successful download, will be be renamed. Not very elegant, but can't think of anything better. – akurtser Jul 22 '10 at 12:19
1  
@akurtser Surely you can check the return code from wget then... it should tell you if you can delete the file "if not found". So no need for temp/renaming. – Mr Shunz Jul 22 '10 at 12:42

As written in the comments, wget -O is more like a shell redirection which always writes into the file regardless of errors.

You can use curl -f instead:

curl -f http://nonexistent/file.jpg -o localfile.jpg

It will not touch the local file if there is an error fetching the file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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