-2

What is a valid way to download files matching a pattern from a directory in a GitHub repository?

bahrep
  • 27,991
  • 12
  • 99
  • 140

2 Answers2

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
  • 48,483
  • 5
  • 42
  • 63
1

If you just want the files, and not the full history, you can:

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,129,465
  • 480
  • 4,036
  • 4,755