bash 如何告诉 curl 在下载前检查文件是否存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10571393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:15:58  来源:igfitidea点击:

How to tell curl to check file existence before download?

bashcurl

提问by kev

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 curlto check file existence before download?

curl下载前如何判断文件是否存在?

I can use this command to prevent curloverride existing images:

我可以使用此命令来防止curl覆盖现有图像:

chmod 000 *.jpg

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

但我不想重新下载它们。

回答by Rony

If the target resource is static, curlhas an option -zto only download a newer target copy.

如果目标资源是静态的,curl则可以选择-z仅下载较新的目标副本。

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

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

回答by Gallaecio

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

不知道用 curl 来做,但你可以在运行 curl 命令之前用 Bash 检查它。

for FILE in FILE1 FILE2 …
do
  if [[ ! -e $FILE ]]; then
    # Curl command to download the image.
  fi
done