How Does Wget Handle Existing Files?

When you use the wget command to download a file that already exists in your destination directory, its default behavior is to save a new copy of the file rather than overwriting the original. It achieves this by automatically appending a numeric suffix (such as .1, .2, etc.) to the end of the new file name. However, wget is highly versatile and offers several command-line flags that allow you to change this behavior, enabling you to overwrite the existing file, skip the download entirely, or resume a partially completed transfer.

Default Behavior: Unique Suffixes

If you run wget without any special flags and the file you are downloading already exists in the target folder, the utility protects your local data from being overwritten.

Alternative Behaviors and Flags

You can modify how wget interacts with existing files depending on your specific workflow or automation needs.

Overwriting Existing Files

If you want to replace the local file with the freshly downloaded version, use the lowercase -O (output document) flag. This forces wget to route the download directly into the specified filename, overwriting whatever is currently there.

wget -O filename.ext URL

Note: Be careful with the -O flag when downloading multiple URLs at once, as it will concatenate all downloads into that single file.

Skipping Already Downloaded Files

If your goal is to avoid wasting bandwidth on files you already have, use the -nc (no-clobber) flag. When this flag is active, wget checks if the file exists and, if it does, immediately terminates the operation without downloading anything.

wget -nc URL

Resuming Interrupted Downloads

If a file exists because a previous download attempt was cut short, you can use the -c (continue) flag. Instead of starting over or creating a .1 file, wget looks at the size of the local file and asks the server to send only the remaining pieces.

wget -c URL

Server-Side Timestamping

Another intelligent option is the -N (timestamping) flag. When used, wget checks the modification date of the file on the remote server. It will only download the file if the remote version is newer than your local copy, making it ideal for maintaining updated backups.

wget -N URL