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?
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
noclobber
option enabled (set +o noclobber
).
-JO
.
Pass --clobber
. This also covers server-provided filename when using -J
.
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 terminalHowever, 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
headerIn your case, if you don't care about the timestamp, it's just:
➸ wget -O myfile.jpg http://example.com/myfile.jpg
curl
version on which system?