While Github is most commonly used to host large code projects, it also functions as a pretty good file host, and sometimes you just want to download a file or two without cloning the entire repo. Github supports this, both from the site and command line.
Downloading a Single File From The Github Website
If you’re fine using your web browser, you can download single files pretty easily. Head over to the file you want to download and click “Raw:”
This will open up a page with a direct link to the file. You can copy/paste from here, but in most browsers, you should be able to right click and select “Save As” to download the file directly.
For code files, it may try to save as .txt, which you will need to fix manually before or after downloading.
Downloading From The Command Line
Since files served from raw.githubusercontent.com, you can download them directly through wget or curl. They’re stored in accessible locations, so if you know the username, repository, and file path, you can download any file on any branch like so:
If you’d rather use Git’s API, you can interact with it more directly, and download files when you aren’t sure of the exact file location. You will need to create a personal access token to use the API, with which you can replace “ACCESS_TOKEN” in this script.
The /repos/{user}/{repo}/contents endpoint will do a different things depending on whether the path points to a directory or a file. If it’s a file, it will return metadata about that file:
If it’s a directory, it will return all the files and subdirectories in an array:
You can then use a JSON parser like jq to pull out the URL and download it. To get the repository file list recursively, you will need to fetch the root tree.
RELATED: How to Work With JSON On the Command Line