Automate GitHub Repository Downloads with a Bash Script

Automating GitHub repository downloads to a local machine with a Bash script.

As a developer, you may find yourself needing to download multiple repositories from GitHub. Whether you do it for backup purposes, or to mitigate platform risk, doing it manually can be time-consuming and tedious. To help with this, I've created a simple Bash script that downloads multiple public repositories from GitHub for a given user. You can find my GitHub repository here.

Requirements

  • Bash shell
  • Curl
  • Unzip

Usage

  1. Clone the repository or copy the script to your local machine.
  2. Open the script in a text editor and modify the USERNAME variable to the desired GitHub username.
  3. Run the script in a terminal with ./github-repo-downloader.sh.

The script will create a folder with the current date and username, download and unzip each repository within that folder.

Customization

Modify the script to suit your needs by changing the USERNAME variable, specifying a different folder name, or adding additional functionality.

#!/bin/bash

# Define the GitHub username and folder name
USERNAME="<GitHub_username_here>"
FOLDER_NAME=$(date +"%Y%m%d")"-"${USERNAME}

# Create a folder with the current date and username
mkdir $FOLDER_NAME

# Get a list of all public repositories for the username using the GitHub API
REPOS=$(curl -s "https://api.github.com/users/${USERNAME}/repos" | grep -o '"full_name": "[^"]*' | awk -F'"' '{print $4}' | awk -F'/' '{print $2}')

# Loop through the repository names and download and unzip each one
for repo in ${REPOS}; do
  # Create a directory for the repository inside the folder
  mkdir "${FOLDER_NAME}/${repo}"
  
  # Download the ZIP file for the repository and save it in the repository directory
  curl -L "https://github.com/${USERNAME}/${repo}/archive/refs/heads/master.zip" -o "${FOLDER_NAME}/${repo}/${repo}.zip"
  
  # Unzip the contents of the ZIP file into the repository directory
  unzip -j "${FOLDER_NAME}/${repo}/${repo}.zip" -d "${FOLDER_NAME}/${repo}"
  
  # Remove the ZIP file
  rm "${FOLDER_NAME}/${repo}/${repo}.zip"
done

Subscribe to alphasec

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe