wget
download location
command line tools
Linux tutorials
file management

How to specify the download location with wget?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

wget saves downloads into the current working directory by default. To change the destination, use -P to specify a directory (keeping the original filename) or -O to specify the full output path including your own filename. These two flags cover virtually every download-location scenario you will encounter.

Understanding when to use each flag, and how they interact with directory creation, recursive downloads, and shell quoting, saves a surprising amount of debugging time in automation scripts and CI pipelines.

-P Sets the Directory Prefix

The -P (or --directory-prefix) flag tells wget which directory to place the downloaded file in. The filename itself comes from the URL or from the server's Content-Disposition header.

bash
wget -P /tmp/downloads https://example.com/files/report.csv

This saves the file as /tmp/downloads/report.csv. The filename report.csv is determined by the URL, not by you.

When downloading multiple files in one command, -P is the natural choice because each file keeps its original name:

bash
1wget -P /tmp/downloads \
2  https://example.com/a.csv \
3  https://example.com/b.csv \
4  https://example.com/c.csv

All three files land in /tmp/downloads/ with names a.csv, b.csv, and c.csv.

-P with Recursive Downloads

During recursive downloads (-r), wget creates subdirectories under the prefix path to mirror the remote directory structure:

bash
wget -r -P /tmp/mirror https://example.com/docs/

This creates a tree like /tmp/mirror/example.com/docs/page1.html. The -P flag sets the root of that tree.

If you want a flat structure without the hostname subdirectory, combine -P with -nH (no host directories):

bash
wget -r -nH -P /tmp/mirror https://example.com/docs/

-O Sets the Full Output Path

The -O flag gives you complete control over where the file lands and what it is called:

bash
wget -O /tmp/downloads/monthly-report.csv https://example.com/files/report.csv

This ignores the URL filename entirely and writes the response body to /tmp/downloads/monthly-report.csv.

-O is the right tool when:

  • The URL does not end in a meaningful filename (for example, an API endpoint like /api/export?format=csv)
  • You need a predictable filename for a downstream script step
  • The server filename is ugly, unstable, or includes query parameters

Writing to Standard Output

A special case of -O is writing to stdout with -O -:

bash
wget -qO - https://example.com/config.json | jq '.version'

The -q flag suppresses progress output so only the downloaded content reaches the pipe. This is a common pattern in shell scripts that need to fetch and process data inline.

Comparison: -P vs -O

Behavior-P /tmp/downloads-O /tmp/downloads/file.csv
Who decides the filenameServer / URLYou
Works with multiple URLsYesNo (last URL wins)
Works with recursive -rYesNo
Creates subdirectoriesYes (with -r)No
Useful for automationWhen original names matterWhen you need a fixed output path

The critical difference: -O with multiple URLs concatenates all responses into a single file. That is almost never what you want, so use -P for multi-file downloads.

Make Sure the Directory Exists

wget does not create arbitrary parent directories for you. If the destination does not exist, the command fails. A safe pattern for scripts is:

bash
mkdir -p /tmp/downloads
wget -P /tmp/downloads https://example.com/files/report.csv

With -O, the same principle applies:

bash
mkdir -p /tmp/downloads
wget -O /tmp/downloads/report.csv https://example.com/files/report.csv

Note that -P with recursive mode (-r) will create subdirectories under the prefix, but the prefix directory itself must already exist.

A Practical Script Example

This shell snippet shows a real-world pattern for downloading a daily export file with a timestamped name:

bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4TARGET_DIR="$HOME/download-cache"
5URL="https://example.com/data/export.json"
6TIMESTAMP=$(date +%Y%m%d-%H%M%S)
7
8mkdir -p "$TARGET_DIR"
9wget -O "$TARGET_DIR/export-${TIMESTAMP}.json" "$URL"
10
11echo "Saved to $TARGET_DIR/export-${TIMESTAMP}.json"

The -O flag produces a predictable, timestamped filename that downstream processing can reference.

For a retry-capable version with logging:

bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4TARGET_DIR="$HOME/download-cache"
5URL="https://example.com/data/export.json"
6LOG_FILE="$TARGET_DIR/wget.log"
7
8mkdir -p "$TARGET_DIR"
9wget --tries=3 --timeout=30 \
10     -o "$LOG_FILE" \
11     -O "$TARGET_DIR/export.json" \
12     "$URL"

Notice that lowercase -o writes the log (not the downloaded content) to a file. This is one of the most confusing aspects of wget's flag set.

Quoting Paths with Spaces

If the destination path contains spaces, quote the argument:

bash
wget -O "$HOME/My Downloads/report.csv" https://example.com/files/report.csv

This is standard shell behavior, but it surfaces frequently as a download problem when it is really a path-parsing issue. Always double-quote variables and paths in scripts.

wget vs curl Flag Comparison

A lot of confusion comes from mixing wget and curl options. The flags overlap in name but not in meaning:

Actionwgetcurl
Set output filename-O filename-o filename
Set output directory-P directoryNo direct equivalent (use -o dir/file)
Use remote filenameDefault behavior-O (uppercase)
Write log to file-o logfile (lowercase)No direct equivalent (use 2>)
Quiet mode-q-s

The uppercase/lowercase swap between wget -O (output file) and curl -O (use remote name) is the single most common source of confusion.

Common Pitfalls

Using lowercase -o instead of uppercase -O is the most frequent mistake. In wget, lowercase -o writes a log file, not the downloaded content. Your download appears to succeed, but the output file contains progress logs instead of the actual data.

Using -O with multiple URLs causes wget to concatenate all responses into a single file. Each subsequent download overwrites or appends to the same output. Use -P for multi-file downloads instead.

Assuming -P lets you rename the file is another common error. It does not. -P only changes the directory prefix while the filename is determined by the URL or server response.

Forgetting to create the destination directory before running the command causes failures that look unrelated to the URL itself. Always use mkdir -p before wget in scripts.

Finally, mixing wget and curl flag meanings in the same mental model leads to subtle bugs that are hard to track down. If a command copied from memory does not work, verify which tool you are actually using.

Summary

  • Use -P to choose the destination directory while preserving the original filename from the URL.
  • Use -O to choose the full output path and filename yourself.
  • Never use -O with multiple URLs unless you intentionally want concatenated output.
  • Create the target directory with mkdir -p before downloading in scripts.
  • Quote paths that contain spaces using double quotes.
  • Be careful not to confuse wget -O (output file) with wget -o (log file), or with curl's differently-cased equivalents.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions