25

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
  • 8
    Cannot reproduce: what curl version on which system? Commented Aug 28, 2011 at 16:34

3 Answers 3

32

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). Commented Aug 28, 2011 at 17:19
  • 4
    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. Commented May 5, 2022 at 18:10
10

Pass --clobber. This also covers server-provided filename when using -J.

2
  • ... since 2022 -- long after the previous answers were written. Commented Jan 3, 2024 at 0:59
  • 5
    It's still useful to mention it for anyone reaching here from Google (like myself). Commented Jan 4, 2024 at 3:57
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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.