Reputation: 1003
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
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 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).
Upvotes: 2
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
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