How to skip already existing files when downloadin

2019-04-04 00:27发布

I want to curl to download a link, but I want it to skip files that already exist. Right now, the line of code I have will continue to overwrite it no mater what:

curl '$url' -o /home/$outputfile &>/dev/null &

How this can be achieved?

4条回答
smile是对你的礼貌
2楼-- · 2019-04-04 01:01

You could just put your call to curl inside an if block:

if ! [ -f /home/$outputfile ]; then
  curl -o /home/$outputfile "url"
fi

Also note that in your example, you've got $url inside single quotes, which won't do what you want. Compare:

echo '$HOME'

To:

echo "$HOME"

Also, curl has a --silent option that can be useful in scripts.

查看更多
疯言疯语
3楼-- · 2019-04-04 01:07

The curl may support skipping the files when you use it with -O and -J, but its behaviour is inconsistent.

The -J (--remote-header-name) basically tells the -O (--remote-name) option to use the server-specified Content-Disposition filename instead of extracting a filename from the URL. In that way the curl doesn't really know what file name the server will return, so it may ignore the existing file for a safety precaution.

Source: Re: -J "Refusing to overwrite..."

For example:

$ curl -LJO -H 'Accept: application/octet-stream' 'https://api.github.com/repos/x/y/releases/assets/12345
Warning: Refusing to overwrite my_file.bin: File 
Warning: exists
curl: (23) Failed writing body (0 != 16384)

However as mentioned already, its behaviour is unpredictable and it doesn't work for all the files.

查看更多
来,给爷笑一个
4楼-- · 2019-04-04 01:11

You can use curl option -C -. This option is used to resume a broken download, but will skip the download if the file is already complete. Note that the argument to -C is a single dash. A disadvantage might be that curl still briefly contacts the remote server to ask for the file size.

查看更多
对你真心纯属浪费
5楼-- · 2019-04-04 01:16

Use wget with --no-clobber instead:

-nc, --no-clobber: skip downloads that would download to existing files.

Example:

wget -nc -q -O "/home/$outputfile" "$url" 
查看更多
登录 后发表回答