Saffik
Saffik

Reputation: 1003

make wget overwrite a file if already exist and download it everytime regardless it being changed on remote server

When I do wget twice, it does not overwrite the file but instead appends a .1 to the name.

$ wget https://cdn.sstatic.net/askubuntu/img/logo.png
...
Saving to: ‘logo.png’
...

$ wget https://cdn.sstatic.net/askubuntu/img/logo.png
...
Saving to: ‘logo.png.1’
...

I want wget to overwrite the logo.png file:

Is there still a way to do it? I searched but couldn't find an example?

Upvotes: 4

Views: 4578

Answers (3)

xhienne
xhienne

Reputation: 6134

There is a way to do this with wget options but it's a bit of a hack:

wget --page-requisites --no-host-directories --cut-dirs=1000 URL

Explanation:

  • --page-requisites forces a download, clobbering existing files, but creates a tree hierarchy
  • --no-host-directories prevents wget from creating a top dir named after the host in the URL
  • --cut-dirs=1000 cuts the 1000 first directory components, effectively putting the downloaded file in the current directory

Another less hacky solution is to create a bash function for this:

wget_clobber() {
    local url=${1:?First parameter is a URL}
    wget --output-document="${url##*/}" "$url"
}

Explanation: we just use the --output-document (or -O) to force wget to write to the file named after the last part of the URL (${url##*/} is equivalent to $(basename "$url") and you can use the latter as well).

Upvotes: 2

aekiratli
aekiratli

Reputation: 538

wget doesn't let you overwrite an existing file unless you explicitly name the output file on the command line with option -O.

wget --backups=1 https://cdn.sstatic.net/askubuntu/img/logo.png

This is not what you requested but might work. Renames original file with .1 suffix and writes new file

Upvotes: 0

pii_ke
pii_ke

Reputation: 2881

Download in a new temporary directory and then move the files to target directory.

DIR=$(mktemp --directory);
wget --directory-prefix="$DIR" "$URL";
mv "$DIR/"* .

Upvotes: 0

Related Questions