Last active
July 21, 2021 20:18
-
-
Save kiliman/74554ee691a28b3a69ced572a9305fde to your computer and use it in GitHub Desktop.
Bash script to download folder from GitHub repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
if [ -z "$1" ]; then | |
echo USAGE: "$0" URL [BRANCH] | |
exit | |
fi | |
url="$1" | |
# default to branch master | |
branch=master | |
# optional 2nd parameter is branch | |
if [ -n "$2" ]; then | |
branch="$2" | |
fi | |
# split url by / | |
IFS=/ read -r -a parts <<< "$url" | |
user="${parts[3]}" | |
repo="${parts[4]}" | |
# get folder segments starting at 5 to end of array | |
folders=("${parts[@]:5}") | |
folder="$(IFS=/; printf '%s' "${folders[*]}")" | |
# strip off parent folders above specific folder | |
strip="$((${#parts[@]} - 5))" | |
# download git archive | |
# pipe to tar to extract (strip off parent folder) | |
curl -L "https://github.com/${user}/${repo}/archive/${branch}.tar.gz" \ | |
| tar -xv --strip-components=${strip} "${repo}-${branch}/${folder}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment