Skip to content

/flags/wget/wget-o-output-document

wget -O file (--output-document): save the download under another name

The commands on this page have not been executed by our verification runner yet.updated
tool
wget
command
wget
flag
-O FILE, --output-document=FILE
default
the file name from the URL (or index.html), saved in the current directory

wget -O app.tar.gz https://example.org/download?id=42 saves the downloaded content as app.tar.gz instead of a name derived from the URL (here it would be download?id=42). -O - writes the content to standard output, so it can be piped: wget -qO- https://example.org/data.json | jq .. Do not confuse it with lowercase -o FILE, which writes wget's log messages to FILE.

Examples

bash
wget -O app.tar.gz "https://example.org/download?id=42"     # choose the file name
wget -O /tmp/app.tar.gz https://example.org/app.tar.gz       # choose the directory too
wget -qO- https://example.org/version.txt                    # print to stdout, no progress
wget -q -O - https://example.org/install.sh | less           # read a script before running it
wget -P downloads/ https://example.org/app.tar.gz            # keep the URL name, change the directory

-O, -o, -P and --content-disposition

OptionEffect
-O FILEWrite the downloaded data to FILE (- = standard output)
-o LOGFILEWrite wget messages to LOGFILE instead of standard error
-a LOGFILEAppend messages to LOGFILE
-P DIRSave files into DIR, keeping their names
--content-dispositionUse the file name the server sends in the Content-Disposition header
-qQuiet: no progress or messages (often combined as -qO-)

How -O behaves

  • With several URLs, -O FILE concatenates all of them into the one file; it does not name each download.
  • An existing FILE is overwritten (truncated), even if the download then fails, so a failed wget -O file can leave an empty file behind.
  • -O together with -N (timestamping) or -r (recursive) does not work as expected: wget warns, because the file name no longer matches the remote one. Use -P with recursive downloads.
  • On a failed request wget still exits non-zero (8 for an HTTP error response such as 404), so test the exit status, not the existence of the file.
bash
if wget -q -O config.json https://example.org/config.json; then
  echo "downloaded"
else
  echo "wget failed with $?" >&2; rm -f config.json
fi

The curl equivalents are curl -o FILE URL (lowercase in curl) and curl -O URL to keep the remote name.

Sources