How to Make Wget Use Numerical Suffixes?

When downloading multiple files or running automated scripts, the standard behavior of wget can sometimes result in existing files being overwritten or saved with confusing extensions. This article provides a quick overview of how to configure wget to automatically append numerical suffixes to duplicate files, ensuring your existing data remains safe. We will explore the default behavior of the tool, the specific command-line flags required to enable numeric suffixes, and how to apply these settings globally for a seamless workflow.

Understanding Wget’s Default Behavior

By default, if you download a file named document.pdf twice in the same directory, wget will automatically append a numerical suffix to the new file, saving it as document.pdf.1.

However, this behavior changes depending on the flags you use. For instance, if you use the -O (or --output-document) flag to force a specific filename, wget will completely overwrite the existing file without warning.

Preventing Overwrites with the Right Flags

To ensure wget always saves duplicate files with a clean, numerical suffix instead of overwriting them, you need to combine specific options depending on your exact use case.

1. The Standard Auto-Suffix (Default Behavior)

If you are simply downloading a file without forcing a new name, just run the standard command:

wget https://example.com/file.zip

If file.zip already exists, wget will automatically save the new download as file.zip.1.

2. Maintaining Suffixes with Backups

If you want to explicitly tell wget to back up an existing file before writing a new one, you can use the --backups option. This is particularly useful when you want to control how many backup generations to keep.

wget --backups=3 https://example.com/file.zip

3. Handling the -O Overwrite Issue

If you must use the -O flag to rename a file but want to prevent it from destroying an existing file, wget does not have a built-in “suffix” workaround for that specific flag. Instead, you should use the -P (prefix) directory flag to change the destination, or use a shell script to check for the file’s existence first:

# Downloads to a specific directory instead of renaming, preserving auto-suffix
wget -P /path/to/directory https://example.com/file.zip

Automating the Configuration

If you want wget to strictly adhere to backup rules without typing the flags every time, you can add them to your global or local configuration file (.wgetrc).

  1. Open your configuration file using a text editor:
nano ~/.wgetrc
  1. Add the following line to enable backup suffixes by default:
backups = 1
  1. Save and exit. wget will now automatically apply this logic to your future downloads.