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?
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 directoryAnother 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).
Download in a new temporary directory and then move the files to target directory.
DIR=$(mktemp --directory);
wget --directory-prefix="$DIR" "$URL";
mv "$DIR/"* .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With