/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
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
| Option | Effect |
|---|---|
| -O FILE | Write the downloaded data to FILE (- = standard output) |
| -o LOGFILE | Write wget messages to LOGFILE instead of standard error |
| -a LOGFILE | Append messages to LOGFILE |
| -P DIR | Save files into DIR, keeping their names |
| --content-disposition | Use the file name the server sends in the Content-Disposition header |
| -q | Quiet: no progress or messages (often combined as -qO-) |
How -O behaves
- With several URLs,
-O FILEconcatenates 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 filecan leave an empty file behind. -Otogether with-N(timestamping) or-r(recursive) does not work as expected: wget warns, because the file name no longer matches the remote one. Use-Pwith 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.
if wget -q -O config.json https://example.org/config.json; then
echo "downloaded"
else
echo "wget failed with $?" >&2; rm -f config.json
fiThe curl equivalents are curl -o FILE URL (lowercase in curl) and curl -O URL to keep the remote name.
Sources
Related pages
flags · wgetwget -N (--timestamping): download only if the remote file is newerwget -N downloads a file only when the server copy is newer or a different size, using Last-Modified and If-Modified-Since. Messages, -O and -nc conflicts.flags · wgetwget -c (--continue): resume a partially downloaded filewget -c resumes an interrupted download by requesting only the missing bytes with an HTTP Range header. It needs server support; --tries retries on failure.commands · wgetwget --mirror --convert-links --page-requisites: download a website for offline usewget --mirror -p -k -E --no-parent URL downloads a site recursively with its images and CSS, rewrites links for offline use and stays below the start folder.flags · curlcurl -f (--fail): exit with an error on HTTP 4xx and 5xxcurl -f makes curl exit with code 22 when the server answers 400 or higher, instead of exiting 0 and printing the error page. --fail-with-body keeps the body.