Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using curl in this syntax:

curl -o myfile.jpg http://mysite.com/myfile.jpg

If I run this command twice, I get two files:

myfile.jpg
myfile-1.jpg

How can I tell CURL that I want it to overwrite the file if it exists?

share|improve this question

migrated from serverfault.com Aug 28 '11 at 16:23

This question came from our site for system and network administrators.

3  
Cannot reproduce: what curl version on which system? – enzotib Aug 28 '11 at 16:34
up vote 8 down vote accepted

Use:

curl http://mysite.com/myfile.jpg > myfile.jpg
share|improve this answer
1  
And beforehand, make sure you do not have the noclobber option enabled (set +o noclobber). – rozcietrzewiacz Aug 28 '11 at 17:19

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.