314

I have a crontab that wgets a PHP page every five minutes (just to run some the PHP code), and I want to send the output of the request to standard out, while sending the normal wget output to /dev/null (or otherwise hide it). I couldn't find it in the wget manual.

I'm looking for something like:

wget -o stdout http://whatever.com/page.php > /dev/null

Anyone know?

5
  • 7
    Mistitled, should be "How do you redirect wget to null?".
    – Bob Stein
    Commented Dec 30, 2014 at 13:47
  • 3
    @BobStein-VisiBone I think it is titled correctly. I wanted the thing that is wget-ed to go to a stdout, and the normal stdout to go to null (i.e. ignore what it usually prints, and instead print the response body). Commented Dec 31, 2014 at 1:24
  • 8
    Oh! I stand corrected. I have started using wget http://example.com/page.php -qO- (That's a capital Oh.) That standard-outputs ONLY the response body. Is that what you wanted?
    – Bob Stein
    Commented Dec 31, 2014 at 4:32
  • In case it helps someone (as per how I landed here), On a terminal of an AWS Cloud9 its wget -O - 169.254.169.254/latest/meta-data/public-hostname -q which gets me the public DNS (as doing web dev)
    – KevinY
    Commented Jan 14, 2021 at 20:43
  • Dealing with ampersands, etc. in the URL (e.g. https://superuser.com/questions?tab=newest&page=542&pagesize=50): See e.g. How can I download weblinks using wget which do not end with .html? Commented Feb 1, 2021 at 14:44

7 Answers 7

312

wget -O - http://whatever.com/page.php > /dev/null

or, if you want to redirect standard error output also:

wget -O - http://whatever.com/page.php > /dev/null 2>&1

or, for codegolf :-)

wget -O-

7
  • 31
    You may want to add -nv to avoid the progress indicator overwriting the output. Commented Nov 10, 2015 at 15:25
  • 1
    This approach has a problem - if response status is not 200, it doesn't print body. Any thoughts to resolve it?
    – Imaskar
    Commented Aug 13, 2018 at 9:24
  • 3
    See answer from Martin Wang - you need to add -q to actually see the output. Commented Mar 3, 2020 at 12:03
  • 3
    I want to output IP address from website so i just put wget -q -O - https://api.ipify.org and it's output it to the shell .
    – Salem F
    Commented Aug 21, 2020 at 18:34
  • 1
    This redirects all output to /dev/null: wget -O - whatever.com/page.php > /dev/null You want the following (to redirect stderror only): wget -O - whatever.com/page.php 2> /dev/null
    – NeoH4x0r
    Commented Jan 18, 2022 at 21:48
252

A simpler version

wget -qO- http://example.com

equivalent to

wget -q -O - http://example.com

where

  • -q turns off the output of log, including error information
  • -O -, equivlalent to -O /dev/stdout, means dump the web page to a file named /dev/stdout.
2
  • 4
    The first one works with BusyBox's wget, which is very helpful. I don't think it likes having a space after the O
    – zymhan
    Commented Jul 19, 2019 at 15:10
  • 1
    wget --quiet --output-document=/dev/stdout http://localhost:8080/commands/srvr | grep "server_state" to show zookeeper state
    – Nick Dong
    Commented May 21, 2022 at 13:12
21
wget -qO /dev/null http://whatever.com/page.php
  • -q to make it quiet
  • -O /dev/null to ignore the page contents
0
13

You can also try:

wget -q -O - http://whatever.com/page.php > /dev/null 

the -q will make it "quiet"

Or have the file go to some temp html page that you don't mind having. whatever.com/tempFile.html

3
wget -O /dev/null http://example.com/
3
  • It works on my mac with wget 1.12.
    – Simon
    Commented Aug 9, 2011 at 22:29
  • 1
    Could you provide some explanation about what your code does? Commented Nov 5, 2011 at 1:14
  • -O means "write output to this file" Commented Nov 14, 2017 at 23:57
0

I could never get this to work just right with wget, but if you have python and the requests library on your system, this might work better for you:

python -c "import requests;print(requests.get('http://whatever.com/page.php').text)"
0

I use this approach to get the output of the request:

wget http://localhost:8000/live -O test && cat test && echo "\n" && rm test

One disadvantage is however that you need write rights. So you might use the tmp folder for writing the temporary result.

1
  • 1
    How is this better than accepted answer, given ~13 years ago ?
    – Toto
    Commented Jun 10 at 16:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .