37

How can I have wget print errors, but nothing otherwise?

In the default behavior, it shows a progress bar and lots of stuff.

In the --no-verbose version still prints one line per downloaded file, this I don't want.

The --quiet option causes it to be totally quiet, even in the case of an error, it doesn't print anything.

Is there a mode in which it prints errors, but nothing else?

6 Answers 6

23

There are very good answers in this question, be sure to check them out, but what I've done is this:

wget [wget options] 2>&1 | grep -i "failed\|error"
1
  • This was the only answer that worked for me with 16.04 ubuntu bash.
    – Ligemer
    Oct 7, 2016 at 23:08
20

Use curl, no point guessing what every error will look like.

[wizard@laptop ~] curl -s -S http://www.google.coccm/ > /dev/null && echo "TRUE"
curl: (6) Couldn't resolve host 'www.google.coccm'
[wizard@laptop ~]$ curl -s -S http://www.google.com/ > /dev/null && echo "TRUE"
TRUE

-s/--silent

Silent mode. Don’t show progress meter or error messages. Makes Curl mute.

-S/--show-error

When used with -s it makes curl show error message if it fails.

And if you need stderr on stdout for some reason.

curl -s -S http://www.google.coccm/  2>&1 1> /dev/null
5
  • 1
    Unfortunately this doesn't handle typical HTTP errors (eg. 404 errors are suppressed this way), whereas the hacky wget method does.
    – mboratko
    Jan 19, 2017 at 5:08
  • Downvote: Although your suggestion is plausible, it does not answer the question. Apr 18, 2017 at 11:40
  • Add -f/--fail to display an error message if the server returns an error code (e.g. 404) Jun 11, 2017 at 13:27
  • 1
    This could be a great answer if, the question was about curl, or if curl had feature parity with wget. Unfortunately neither is the case..
    – dfc
    Aug 31, 2017 at 18:42
  • nice advise and worth to know, still not an answer
    – shabunc
    May 11, 2021 at 19:23
5

I don't see an option for that. Do you need to know what the error is, or just if it happened? If you happen to just need to know if there was error, you can use the exit status.

if ! wget -o /dev/null www.google.com/flasfsdfsdf; then
    echo 'Oops!'
fi

Or, maybe:

if ! wget -o logfile www.google.com/flasfsdfsdf; then
    cat logfile
fi

And you can change the cat to a grep command if you want to get fancy...

2
  • 3
    More simple way: wget -o logfile <url> || cat logfile
    – kolypto
    Oct 4, 2009 at 11:20
  • The more simple way is worse since you probably want to exit or do something different on error, in addition to displaying the error message. Jun 20, 2016 at 0:23
3
OUT=`wget --no-verbose -O /tmp/a http://example.com/ 2>&1` || echo $OUT

works. But it always truncates the output file, which you may or may not want.

Curl is better:

curl --fail --silent --show-error -o /tmp/a http://example.com

In case of an error it does not modify the output file.

2

Redirect standard output to /dev/null, but keep the error output in your choice of shell.

In bash this would be:

wget [wget options] > /dev/null

Edit: So wget misbehaves. If all errors contain the word "error" in them you could pipe to grep

wget [wget options] 2>&1 | grep -i "error"
4
  • 1
    wget seems to send everything to stderr.
    – Pablo
    Oct 2, 2009 at 19:16
  • I added another option using grep to only output errors
    – Ben S
    Oct 2, 2009 at 19:19
  • stderr doesn't go through the pipe without help. Oct 2, 2009 at 19:34
  • 1
    In the end I used wget [wget options] 2>&1 | grep -i "failed\|error"
    – Pablo
    Oct 2, 2009 at 19:37
1

Since wget outputs all messages on stderr, you have to use redirection before you can pipe it to grep:

wget [options] 2>&1 | grep "^wget:"

This assumes that wget begins its error lines with "wget:".

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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