Skip to content

Instantly share code, notes, and snippets.

@michfield
Last active July 25, 2021 05:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save michfield/4525251 to your computer and use it in GitHub Desktop.
Save michfield/4525251 to your computer and use it in GitHub Desktop.
Clone all GitHub repositories of a user #bash

Bash / Clone all GitHub repositories of a user

See my GitHub API current rate limits:

curl -sI "https://api.github.com/users/cookbooks/repos" | grep "^X-RateLimit" Any left? curl -sI "https://api.github.com/users/cookbooks/repos" | sed -nr 's/^X-RateLimit-Remaining: (.*)$/\1/p'

Or even simpler:
curl -s "https://api.github.com/rate_limit"

See how much data there is (paginated), for some user:

curl -sI "https://api.github.com/users/cookbooks/repos?page=1&per_page=100" | grep "^Link:"

Extract that (number of pages / last page number):
curl -sI $url | sed -nr 's/^Link:.*page=([0-9]+)&per_page=100>; rel="last".*/\1/p'

And now, the complete list extract:

usr="cookbooks";
url="https://api.github.com/users/${usr}/repos";
num=$(curl -sI "$url?page=1&per_page=100" | sed -nr 's/^Link:.*page=([0-9]+)&per_page=100>; rel="last".*/\1/p');
for ((i=1;i<=$num;i++)); do ( curl -s "$url?page=${i}&per_page=100" | grep "clone_url" | sed -nr 's/.*clone_url": "(.*)",/git clone \1/p' ); done >clone_allrepos_${usr}.sh

Execute created .sh when you are ready
./clone_allrepos_${usr}.sh
@csu
Copy link

csu commented Jun 3, 2016

I get:

sed: illegal option -- r
usage: sed script [-Ealn] [-i extension] [file ...]
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
(23) Failed writing body
clone_all.sh: line 4: ((: i<=: syntax error: operand expected (error token is "=")

@cachrisman
Copy link

@csu you need to use gnu sed. If you are on a mac, you can fix this with brew install gnu-sed and follow the instructions in the caveats.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment