-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-download_dir.sh
More file actions
49 lines (43 loc) · 1.13 KB
/
github-download_dir.sh
File metadata and controls
49 lines (43 loc) · 1.13 KB
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
42
43
44
45
46
47
48
49
#!/bin/bash
# Download files one-by-one of a single directory from GitHub
# Note: If repo is not that large, this will be much slower than the whole repo tarball.
# Usage: github_download.sh URL_TO_DIRECTORY
url=$1
user=$( cut -d/ -f4 <<< $url )
repo=$( cut -d/ -f5 <<< $url )
branch=$( cut -d/ -f7 <<< $url )
path=$( cut -d/ -f8- <<< $url )
urlcontent=https://api.github.com/repos/$user/$repo/contents/$path?ref=$branch
subdir=$( basename $path )
dirtarget=$PWD
fileGet() {
dirtarget+=/$1
mkdir -p $dirtarget
lines=$( curl -s $urlcontent )
types=$( grep '"type":' <<< $lines | cut -d'"' -f4 )
names=( $( grep '"name":' <<< $lines | cut -d'"' -f4 ) )
readarray -t download_url <<< $( grep '"download_url":' <<< $lines | cut -d'"' -f4 )
dirs=
i=0
for t in $types; do
name=${names[i]}
if [[ $t == file ]]; then
echo File: $name
curl -LO ${download_url[i]} --output-dir $dirtarget
else
dirs+="$name "
fi
(( i++ ))
done
if [[ $dirs ]]; then
for d in $dirs; do
echo Subdirectory: $d
urlcontent=${urlcontent/\?*}/$d?ref=$branch
fileGet $d
done
fi
}
fileGet $subdir
echo "
Download successfully to: $subdir
"