What is a valid way to download files matching a pattern from a directory in a GitHub repository?
Asked
Active
Viewed 220 times
2 Answers
2
The best way to do this is to clone the repository locally and then extract the files you want. You could also download an archive and extract only those files you want, assuming you only care about a single revision.
GitHub doesn't otherwise offer a way to extract only some files. Trying to script this using the API would be far less efficient and take far more time than just downloading a shallow clone or an archive and extracting client side.

bk2204
- 64,793
- 6
- 84
- 100
-
I don't want to do this because the repository I am interested in is full of junk and even a shallow clone is more than 10G :). And all I want is to download a handful of small files scattered around the repo. But too many to do manually one-by-one. – Ben Farmer Mar 22 '23 at 04:42
1
If you just want the files, and not the full history, you can:
- download the GitHub repository archive (tar.gz)
- extract the files matching the pattern from the archive
That is, in one command (to do in a new empty local folder, for testing):
curl -L https://api.github.com/repos/octokit/octokit.rb/tarball |\
tar -xz --wildcards "*.md"
This creates a folder named after the Git SHA1 of the repository:
C:\Users\vonc\git\test>cd octokit-octokit.rb-be7c105
C:\Users\vonc\git\test\octokit-octokit.rb-be7c105>l
total 52K
-rw-r--r-- 1 vonc 197609 1.3K May 18 23:27 RELEASE.md
-rw-r--r-- 1 vonc 197609 29K May 18 23:27 README.md
-rw-r--r-- 1 vonc 197609 1.1K May 18 23:27 LICENSE.md
-rw-r--r-- 1 vonc 197609 1.2K May 18 23:27 CONTRIBUTING.md
-rw-r--r-- 1 vonc 197609 3.3K May 18 23:27 CODE_OF_CONDUCT.md

VonC
- 1,262,500
- 529
- 4,410
- 5,250