Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell curl to check file existence before download?

Tags:

bash

curl

I use this command to download a series of images:

curl -O --max-time 10 --retry 3 --retry-delay 1 http://site.com/image[0-100].jpg

Some images are corrupted, so I delete them.

for i in *.jpg; do jpeginfo -c $i || rm $i; done

How to tell curl to check file existence before download?

I can use this command to prevent curl override existing images:

chmod 000 *.jpg

But I don't want to re-download them.

like image 347
kev Avatar asked May 13 '12 11:05

kev


People also ask

How do you use curl command to download?

How to download a file with curl command. The basic syntax: Grab file with curl run: $ curl https://your-domain/file.pdf. Get file using ftp or sftp protocol: $ curl ftp://ftp-your-domain-name/file.tar.gz.

How do you check connectivity with curl command?

To use curl to test basic network connectivity, you need to know several things: The remote server name or IP address. The protocol for the service to be tested (HTTP, FTP, SMTP, etc.) The port number for the network application you want to test.

Does curl overwrite files?

curl attempts to cut off the directory parts from any given file name in the header to only store files in the current directory. It will overwrite a local file using the same name as the header specifies.


2 Answers

If the target resource is static, curl has an option -z to only download a newer target copy.

Usage example:

curl -z image0.jpg http://site.com/image0.jpg

An example for your case:

for i in $(seq 0 100); do curl -z image$i.jpg -O --max-time 10 --retry 3 --retry-delay 1 http://site.com/image$i.jpg; done

like image 61
Rony Avatar answered Oct 06 '22 01:10

Rony


No idea about doing it with curl, but you could check it with Bash before you run the curl command.

for FILE in FILE1 FILE2 …
do
  if [[ ! -e $FILE ]]; then
    # Curl command to download the image.
  fi
done
like image 45
Gallaecio Avatar answered Oct 05 '22 23:10

Gallaecio