Skip to content

Instantly share code, notes, and snippets.

@ajanderson1
Last active July 12, 2023 09:52
Show Gist options
  • Save ajanderson1/078b1212166fada6e7b51c80f80651d8 to your computer and use it in GitHub Desktop.
Save ajanderson1/078b1212166fada6e7b51c80f80651d8 to your computer and use it in GitHub Desktop.
Download files from GitHub Gist
#!/bin/bash
# Simple script to download files from a GitHub Gist.
# Script will prompt for a Gist URL (e.g. https://gist.github.com/username/gistId.git), then display the files available to download, alongside row numbers.
# NB: Multiple files can be downloaded at once by entering the row numbers separated by spaces.
# Get Gist URL from the user
echo "Enter the Gist URL:"
read gistUrl
# Strip .git from the URL to get the Gist ID
gistId=${gistUrl%.git}
gistId=${gistId##*/}
# Fetch details about the Gist using the GitHub API
apiUrl="https://api.github.com/gists/$gistId"
# Extract the file information using curl and jq
fileList=$(curl -s "$apiUrl" | jq -r '.files | keys | .[]')
# Create an array of filenames and display the options.
declare -a fileArr
i=1
echo "Available Files:"
while IFS= read -r line
do
echo "$i: $line"
fileArr[i]=$line
i=$((i+1))
done <<< "$fileList"
# Ask the user which files they want to download.
echo "Enter the numbers of the files to download (separated by spaces):"
read -a fileNums
# Download the selected files with curl.
for num in "${fileNums[@]}"
do
selectedFile=${fileArr[$num]}
rawGistUrl="https://gist.githubusercontent.com/raw/$gistId"
curl -O "$rawGistUrl/$selectedFile"
echo "Downloaded $selectedFile"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment