21

I am trying to download a .zip file from GitHub using the command line in Ubuntu. I am using wget command for it on a remote Ubuntu system.

I run wget <link> where <link> is the address bar link of the file which I want to download. It ends with archive.zip?ref=master.

Now, when I am executing the command, it is downloading a file with text/html type and not the .zip file which I want.

Please tell me how to get the link to be given as the parameter of wget. Right now, I am just copying the link address of the button (using right click) and writing that as a wget parameter.

Melebius
  • 11,121
  • 8
  • 50
  • 77
UbuntuCoder
  • 313
  • 1
  • 2
  • 6
  • can you tell the exact command you are pasting? – kashish Jul 26 '17 at 05:10
  • 1
    Does your URL really ends with .zip? – kashish Jul 26 '17 at 05:11
  • @kashish No the URL does not end with .zip . I am trying to download a project from github and it has a download button giving zip and other options. So, I'm trying from there. it ends with archive.zip?ref=master – UbuntuCoder Jul 26 '17 at 05:13
  • To download a github project, you can use `git clone ` – kashish Jul 26 '17 at 05:19
  • 2
    If I'm not mistaken it possible to download a repository as Zip-file using a following address: `https://github.com/{user}/{repo}/archive/{branch}.zip` – Alex Chermenin Jul 26 '17 at 08:22
  • @AlexChermenin that's true but only opening it in a browser and not using wget (see my answer) – derHugo Jul 26 '17 at 11:04
  • @derHugo what's the difference between using wget + url and opening the url in a browser? – Alex Chermenin Jul 26 '17 at 11:56
  • I assume you did not read my answer here below... on a browser you may be redirected/forwarded to another URL .. with wget this will not happen but you will get whatever file the URL points to – derHugo Jul 26 '17 at 15:18
  • @derHugo It's ok when wget received a response `302` and continue downloading with new address. https://unix.stackexchange.com/a/74338 – Alex Chermenin Jul 27 '17 at 07:57
  • Because in this case with sourceforge you were redirected instantly. Github redirects you after buiding the zip file. wget doesn't whait if there's a redirect later or not it just downloads the actual file. – derHugo Jul 27 '17 at 10:39
  • @derHugo Did you try? I tried with several different repositories and branches and every time i receive the right result. What am I doing wrong? – Alex Chermenin Jul 27 '17 at 12:15
  • yes and all I get is: `Resolving github.com (github.com)... 192.30.253.112, 192.30.253.113 Connecting to github.com (github.com)|192.30.253.112|:443... connected. HTTP request sent, awaiting response... 404 Not Found 2017-07-27 14:40:20 ERROR 404: Not Found.` additional wget doesn't know your credentials so you would have to provide them as text which just isn't good – derHugo Jul 27 '17 at 12:41

4 Answers4

20

It does work, if you use the correct URL.

For a GitHub repo, there's a zip at https://github.com/<user>/<repo>/archive/<branch>.zip, so you can download it with:

wget https://github.com/<user>/<repo>/archive/<branch>.zip

This downloads the zipped repo for a given branch. Note that you can also replace the branch by a commit hash.

Using cURL

curl -L -O https://github.com/<user>/<repo>/archive/<branch>.zip

cURL's -L flag follows redirects - it's a default in wget. -O is advised by Phil Gibbins in a comment.

Download a .tgz instead of .zip

You can also download a tarball with:

wget https://github.com/<user>/<repo>/archive/<branch>.tar.gz
Robin Métral
  • 300
  • 2
  • 7
  • 2
    **`curl -L -O ` works on MacOS**; (posting because it's all Unix related) thanks for adding the information about `curl`; I landed here when searching for the equivalent on MacOS, and albeit not hard to install it, `wget` isn't installed by default (my use case limits me from installing). You're advised to add `-O` as "Binary output can mess up your terminal" – Phil Gibbins Jul 08 '20 at 21:25
17

From the comments I saw you actually speak about GitHub.

It won't work like this because:

Downloading a project on GitHub causes the GitHub server to first pack your project as zip and than forwarding you to a temporary link where you get your zip ..

this link will only work for a certain time and than GitHub will delete your zip file from their servers..

So what you get with wget is just the html page which would forward you as soon as your zip file is generated.

As suggested use

git clone http://github.com/<yourRepoLink> <optional local path where to store>

to download the git repo ... If for some reason (e.g. for transfer it to others) you need it explicitly as zip you still could pack it after cloning is finished..

derHugo
  • 3,306
  • 5
  • 30
  • 49
1

2021 Update Download and unzip from GitHub Using wget

Will also expand and delete the archive. Just edit the REPLACE_ values and then copy/paste

zip

GH_USER=REPLACE_WITH_USER \
GH_REPO=REPLACE_WITH_REPO \
GH_BRANCH=REPLACE_WITH_BRANCH \
wget https://github.com/${GH_USER}/${GH_REPO}/archive/refs/tags/${GH_BRANCH}.zip \
-O "${GH_REPO}-${GH_BRANCH}.zip" && \ 
unzip ./"${GH_REPO}-${GH_BRANCH}.zip" && \
rm ./"${GH_REPO}-${GH_BRANCH}.zip"

tgz

GH_USER=REPLACE_WITH_USER \
GH_REPO=REPLACE_WITH_REPO \
GH_BRANCH=REPLACE_WITH_BRANCH \
wget https://github.com/${GH_USER}/${GH_REPO}/archive/refs/tags/${GH_BRANCH}.tar.gz \
-O "${GH_REPO}-${GH_BRANCH}.tar.gz" && \
tar -xzvf ./"${GH_REPO}-${GH_BRANCH}.tar.gz" && \
rm ./"${GH_REPO}-${GH_BRANCH}.tar.gz"
AndrewD
  • 119
  • 3
0

I just did it, but make sure you're using the correct link with URL end with *.zip like what @Robin Métral said above.

The exact steps I did:

  1. Open GitHub repository.
  2. Right click the Download Zip link, and copy the URL.
  3. Use that copied URL with wget in terminal.

I believe GitHub server will accept wget request and treat it as same as request with browser.