forked from paramaggarwal/multipart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipart.sh
41 lines (32 loc) · 860 Bytes
/
multipart.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh
# Author: Param Aggarwal
# Multipart parallel downloader in Shell using cURL
if [[ $# -lt 1 ]]; then
echo "$0: Pass URL to download as argument"
exit 1
fi
url=$1
parts=20
name="$(expr $url : '.*/\(.*\)')"
size="$(curl --head --silent $url | grep Content-Length | sed 's/[^0-9]*//g')"
echo Size: $size
echo Filename: $name
echo Downloading in $parts parts
for (( c=1; c<=$parts; c++ ))
do
from="$(echo $[$size*($c-1)/$parts])"
if [[ $c != $parts ]]; then
to="$(echo $[($size*$c/$parts)-1])"
else
to="$(echo $[$size*$c/$parts])"
fi
out="$(printf 'temp.part'$c)"
echo "curl --silent --range $from-$to -o $out $url &"
curl --silent --range $from-$to -o $out $url &
done
wait
for (( c=1; c<=$parts; c++ ))
do
cat $(printf 'temp.part'$c) >> $name
rm $(printf 'temp.part'$c)
done