How to change filename of a file downloaded with wget?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want wget to save a download under a different local name, the standard solution is the -O option. That lets you control the output path directly, which is more reliable than hoping the server-provided name matches what your script or workflow needs.
Use -O to Set the Local File Name
The simplest case looks like this:
No matter what the remote URL or response headers suggest, the downloaded file is written locally as report.html.
Save Into a Specific Directory Too
-O takes a full path, not just a bare filename, so it is also the normal way to control both name and destination.
This is useful in scripts that stage downloads in known working directories.
Know the Difference Between -O and -o
This is a very common source of mistakes:
- '
-Ocontrols the downloaded file output' - '
-owriteswgetlogs to a file'
That command does not rename the download. It stores wget diagnostic output in wget.log.
Use Shell Variables for Dynamic Names
If the target file name should depend on the date, environment, or script input, build the name in the shell first and pass it to -O.
This is the usual pattern for scheduled downloads and reproducible automation.
When to Keep the Server Name Instead
Sometimes the server sends a useful filename through the URL or headers, and you do not want to override it manually. In those cases, omit -O and let wget choose the default local name.
The right choice depends on your goal:
- use
-Owhen your script needs predictable names - skip
-Owhen the server name is meaningful and should be preserved
Avoid Overwriting by Accident
Because -O is explicit, it is also easy to overwrite an existing file by mistake. If the target path matters, check for existing files before downloading or include a unique suffix such as a timestamp or build ID.
That matters most in automation, where the download command may run repeatedly without manual review.
Prefer Predictable Names in Automation
Predictable local filenames make later steps such as checksum verification, extraction, and deployment much simpler. Renaming at download time is often cleaner than downloading first and then moving the file with a second command.
Quote Paths Consistently
If your generated names include spaces, timestamps, or user-provided text, quote the output path consistently. That small habit prevents avoidable shell-splitting bugs in scripts that run unattended.
It also keeps examples safer to copy.
Common Pitfalls
- Using lowercase
-oinstead of uppercase-O. - Assuming
-Orenames the remote file rather than just the local saved copy. - Forgetting to quote file paths that contain spaces or shell-sensitive characters.
- Overwriting an existing file unintentionally with a fixed output name.
- Forcing a custom name when the script actually needs to preserve the server-provided one.
Summary
- Use
wget -O <filename> <url>to save a download under a custom local name. - '
-Ocan also include the full destination path.' - Do not confuse
-Owith-o, which controls log output. - Shell variables make dynamic naming straightforward in scripts.
- Choose between fixed names and server-provided names based on the workflow you actually need.

