18

I'm using curl in this syntax:

curl -o myfile.jpg http://example.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?

1
  • 6
    Cannot reproduce: what curl version on which system?
    – enzotib
    Aug 28, 2011 at 16:34

3 Answers 3

29

Instead of using -o option to write to a file, use your shell to direct the output to the file:

curl http://example.com/myfile.jpg > myfile.jpg
2
  • 2
    And beforehand, make sure you do not have the noclobber option enabled (set +o noclobber). Aug 28, 2011 at 17:19
  • That doesn't answer the question. That does not make curl overwrite the existing file, that makes the shell overwrite the (potentially) existing file with what curl sent to stdout. So it's incompatible with -JO. May 5 at 18:10
4

I had same problem, and I wanted to reuse the same filename regardless of what the server-side returns. In your case, you can get the filename with basename:

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

Then you can write a helper Bash function like:

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

In my case, however, the filename is not even part of the URL; it comes with the Content-Disposition header. With Curl the command line is:

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

You may ignore the -fSL if you want -- It handles if the server-side returns 302 Redirection. The relevant flags here are:

  • -R for server-side timestamp
  • -J to consider server-side Content-Disposition
  • -O to switch to download behavior instead of dumping on terminal

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

So I end up with a wget solution that's able to do so:

➸ wget -NS --content-disposition 'http://example.com/getData.php?arg=1&arg2=...'
  • -N is to check server side timestamp and overwrite only when server side has new version
  • --content-disposition is to consider the Content-Disposition header
  • wget's default behavior is to download, to the filename given from server side.

In your case, if you don't care about the timestamp, it's just:

➸ wget -O myfile.jpg http://example.com/myfile.jpg
0

Using the double arrow operator >> prevents file overwrite. The top answer is correct and helped me.

1
  • 3
    If the file already exists, double arrow >> will append to it.
    – Cocowalla
    Oct 26, 2021 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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