Skip to content

Instantly share code, notes, and snippets.

@NostraDavid
Created September 18, 2024 17:40
Show Gist options
  • Save NostraDavid/76367f46b1a2cbfd186c1ee29ed3be8b to your computer and use it in GitHub Desktop.
Save NostraDavid/76367f46b1a2cbfd186c1ee29ed3be8b to your computer and use it in GitHub Desktop.
Grab all your repos from github and put them in subfolders per user, using curl, jq and git.
#!/usr/bin/env bash
# Set your GitHub token here
GITHUB_TOKEN="<token goes here>"
# Make the cURL request to GitHub API and store the response
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/user/repos?per_page=100")
# Use jq to parse the JSON response and extract the full_name and ssh_url fields
repos=$(echo "$response" | jq -r '.[] | .full_name + " " + .ssh_url')
# Loop over each repository and clone it
while IFS= read -r repo; do
# Extract full_name and ssh_url
full_name=$(echo "$repo" | awk '{print $1}')
ssh_url=$(echo "$repo" | awk '{print $2}')
# Create directory structure based on the full_name
repo_dir=$(dirname "$full_name")
# Check if the directory exists, if not, create it
if [ ! -d "$repo_dir" ]; then
echo "Creating directory: $repo_dir"
mkdir -p "$repo_dir"
fi
# Clone the repo into the specified folder
if [ ! -d "$full_name" ]; then
echo "Cloning repository: $ssh_url into $full_name"
git clone "$ssh_url" "$full_name"
else
echo "Repository already exists: $full_name"
fi
done <<< "$repos"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment