Skip to content

/commands/wget/wget-download-to-directory

wget -P /path URL: download a file into a specific directory

The commands on this page have not been executed by our verification runner yet.updated
tool
wget
versions
GNU Wget 1.x
os
linux, macos, windows

wget -P /data/downloads https://example.com/files/app.tar.gz saves app.tar.gz in /data/downloads/ instead of the current directory. -P (--directory-prefix) sets the top directory for everything wget writes and creates it if it is missing. The file name still comes from the URL; to choose the name as well, use -O /data/downloads/name.tar.gz instead. Add -nc to skip files that already exist, or -c to resume a partial download.

Commands

bash
# Save into a directory (created if needed)
wget -P ~/Downloads/iso https://releases.example.com/linux/distro-24.04.iso

# Directory and resume, quiet output with a progress bar
wget -q --show-progress -c -P /data/backups https://example.com/backups/db.sql.gz

# Choose the full path and name instead
wget -O /data/backups/latest.sql.gz https://example.com/backups/db.sql.gz

# Use the name sent by the server (Content-Disposition)
wget --content-disposition -P ~/Downloads "https://example.com/download?id=42"

The URLs stand for your own downloads.

Options that decide where and how a file is saved

OptionEffect
-P DIR, --directory-prefix=DIRSave everything under DIR (default: the current directory)
-O FILEWrite the download to exactly FILE (path and name)
-nc, --no-clobberDo not download a file that already exists locally
-N, --timestampingDownload only if the remote file is newer than the local one
-c, --continueResume a partially downloaded file
-x, --force-directoriesRecreate the URL path as directories, for example host/files/app.tar.gz
-nd, --no-directoriesDo not create directories in recursive downloads; everything goes into DIR
--content-dispositionTake the file name from the Content-Disposition header
-q --show-progressHide the log but keep a progress bar (wget 1.16+)

Name clashes

Without -N, -nc or -O, downloading the same URL twice into the same directory keeps the first file and saves the second as app.tar.gz.1, then app.tar.gz.2. With -nc wget refuses to download it again and prints that the file is already there. With -N it compares the local timestamp and size with the server's and downloads only when they differ.

Pitfalls

  • Quotes around URLs with ? or &. Without them the shell interprets & and cuts the URL. Quote the whole URL.
  • -O does not create directories. Unlike -P, the directory part of an -O path must already exist, or wget fails to open the file.
  • Spaces and ~ in paths. -P ~/My Downloads splits at the space; quote it. ~ is expanded by the shell only when it is not inside quotes, so write -P ~/"My Downloads" or -P "$HOME/My Downloads".
  • Windows. Builds of GNU Wget for Windows accept the same options; paths may use either slash. In PowerShell 5.1, wget is an alias of Invoke-WebRequest, so type wget.exe to run the real program.

Check the download

ls -l /data/downloads/ shows the file and its size. Compare it with the size in wget's Length: line or, if the site publishes one, with a checksum: sha256sum /data/downloads/app.tar.gz. wget exits with status 0 on success and 8 when the server returned an error such as 404.

FAQ

Does wget -P create the directory if it does not exist?

Yes. wget creates the directory prefix and any missing parent directories before saving the file, as long as you have permission to create them.

Why did wget save the file as index.html or with a .1 suffix?

wget names the file after the last part of the URL path; a URL ending in / gives index.html. If a file of that name already exists, wget keeps it and saves the new copy as name.1. Use -O to choose a name, -nc to skip existing files, or -N to download only when the remote file is newer.

How do I choose both the directory and the file name?

Give -O a path, wget -O /data/downloads/app.tar.gz URL. -O writes to exactly the file you name, so combine the directory and name there instead of using -P. The directory must already exist for -O.

Sources