How can I specify that curl (via command line) overwrites a file if it already exists?

Use:

curl http://mysite.com/myfile.jpg > myfile.jpg

I met same problem and I want to reuse the same filename whatever server side gives, in your case you may get the filename by basename

➸ basename 'http://mysite.com/myfile.jpg'
myfile.jpg

then you can write a helper bash function like:

➸ function download { name="$(basename $1)"; curl "$1" > "$name"; }
➸ download 'http://mysite.com/myfile.jpg'

but in my case the filename is not even part of the url, it comes with content-disposition; with curl the command line is

$ curl -fSL -R -J -O 'http://some-site.com/getData.php?arg=1&arg2=...'

you may ignore the -fSL it handles if server side gives 302 Redirection; the -R for server side timestamp, -J to consider server side Content-Disposition, and -O to switch to download behavior instead of dumping on terminal;

however, it still suffers refusing to overwrite if file name exists; I want it to overwrite if server side Last-Modified timestamp is newer;

ends up with a wget solution it's able to do so:

$ wget -NS --content-disposition 'http://some-site.com/getData.php?arg=1&arg2=...'

the -N is to check server side tiemstamp and overwrite only when server side has new version; --content-disposition is to consider Content-Disposition header; and wget's default behavior is to download, to the filename given from server side.

in your case, if you won't care timestamp, it's just

$ wget -O myfile.jpg http://mysite.com/myfile.jpg

Tags:

Io

Curl

Filenames