Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers

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
    
@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
    
@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
add comment

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
share|improve this answer
add comment

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.