A HTTP 416 ("requested range not satisfiable") with -C -
is a reasonable response when your file is already complete: The range of content after the current size of the file is an empty set, so while a server could return a 0-byte successful response, it can also state that no response is possible, which is what you're seeing here.
One approach you can take, if your service supports the Content-Length header, is extracting the intended file size from a HEAD request, and comparing that to the current size on-disk:
dest="$HOME/Downloads/$savePath/$filename"
if [[ -e $dest ]]; then
remote_size=$(curl -I "$url" | awk -F: '/^Content-Length:/ { print $2 }')
local_size=$(stat --format=%s "$dest")
if ! [[ $remote_size ]]; then
echo "Unable to retrieve remote size: Server does not provide Content-Length" >&2
elif ! [[ $local_size ]]; then
echo "Unable to check local size: Validate that GNU stat is installed" >&2
elif (( remote_size == local_size )); then
echo "File is complete" >&2
elif (( remote_size > local_size )); then
echo "Download is incomplete -- can probably resume" >&2
elif (( remote_size < local_size )); then
echo "Remote file shrunk -- probably should delete local and start over" >&2
fi
else
echo "File does not exist locally at all" >&2
fi
Note that stat --format
is a GNU extension. If you're running on MacOS, you can install GNU stat as gstat
via MacPorts; see BashFAQ #87 for a detailed discussion on extracting metadata if you don't have GNU tools.