Skip to content

Instantly share code, notes, and snippets.

@asowder3943
Last active August 22, 2022 21:20
Show Gist options
  • Save asowder3943/35e737676830e5796577ce3d2fb9f89c to your computer and use it in GitHub Desktop.
Save asowder3943/35e737676830e5796577ce3d2fb9f89c to your computer and use it in GitHub Desktop.
Downloading All Github Repos of a Given User or Organization
#!/bin/bash
# gharchive is a simple script for downloading all
# github repositories associated with a given user or organization
#
# :dependencies: gh, awk, sed, wget, git
#
# :param: -u pass the user or organization name as it appears on github
# :param: -t pass the download type your wish to preform currently both "clone" and "archive" are supported
#
# :returns: all files are downloaded or cloned into the current directory
#
while getopts u:t: flag
do
case "${flag}" in
u) GH_USER=${OPTARG};;
t) D_TYPE=${OPTARG};;
esac
done
echo "Downloading Archives for Github User: $GH_USER";
# Github Archives can be retrieved at urls with the following form:
# https://github.com/<user>/<repo>/archive/refs/heads/master.zip
#
# Github Repositories can be retrieved from urls with the following form
# https://github.com/<user>/<repo>.git
#
# Prefix for all download types is constant
PREFIX="https:\/\/github.com\/"
case $D_TYPE in
clone)
SUFFIX=".git"
# get list of repos -> select first word from each output <user>/<repo> -> insert prefix -> insert suffix -> run git clone for each url
gh repo list $GH_USER | awk -F" " '{ print $1 }' | sed -e "s/^/${PREFIX}/" | sed -e "s/$/${SUFFIX}/" | xargs -I {} git clone {}
;;
archive)
SUFFIX="\/archive\/refs\/heads\/master.zip"
# get list of repos -> select first word from each output <user>/<repo> -> insert prefix -> insert suffix -> wget list of urls
gh repo list $GH_USER | awk -F" " '{ print $1 }' | sed -e "s/^/${PREFIX}/" | sed -e "s/$/${SUFFIX}/" | xargs wget
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment