Only create file if http status 200 with wget?

2019-04-24 14:57发布

问题:

I have been trying to figure out a way to make wget only create a file if the actual download response is valid, meaning no 404 or 500 status code, only 200.

However, when using the -O option (to specify filename) it will always create the file, with the content of the error page, and I haven't found a way to specify that it should ignore it if it recieves a 404 response-header.

Do I need to check the exit-code and pipe the output or is there a better alternative?

回答1:

Perhaps this is the only answer, other than patching wget (a valid option):

wget -O somefile.html http://example.com/ || rm somefile.html


标签: wget