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.
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.
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:
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:
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):
-O Sets the Full Output Path
The -O flag gives you complete control over where the file lands and what it is called:
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 -:
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 filename | Server / URL | You |
| Works with multiple URLs | Yes | No (last URL wins) |
Works with recursive -r | Yes | No |
| Creates subdirectories | Yes (with -r) | No |
| Useful for automation | When original names matter | When 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:
With -O, the same principle applies:
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:
The -O flag produces a predictable, timestamped filename that downstream processing can reference.
For a retry-capable version with logging:
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:
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:
| Action | wget | curl |
| Set output filename | -O filename | -o filename |
| Set output directory | -P directory | No direct equivalent (use -o dir/file) |
| Use remote filename | Default 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
-Pto choose the destination directory while preserving the original filename from the URL. - Use
-Oto choose the full output path and filename yourself. - Never use
-Owith multiple URLs unless you intentionally want concatenated output. - Create the target directory with
mkdir -pbefore downloading in scripts. - Quote paths that contain spaces using double quotes.
- Be careful not to confuse
wget -O(output file) withwget -o(log file), or withcurl's differently-cased equivalents.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.