How to download a tarball from GitHub in shell script

I know I can use git clone to fetch a repository. Is there easy way to get entire repo as a tarball in shell script?
Instead of the following

git clone https://github.com/copier-org/copier.git

I want

wget https://github.com/copier-org/copier.git

Not working. Hence, asking how can I download it as a tarball from GitHub?

Downloading a tarball or zip file from GitHub is easy. We can either use the curl or wget command.

Turn git url to zip or tar url

input="https://github.com/copier-org/copier.git"
out="${input%%.git}/archive/refs/heads/master"
echo ${out}.{zip,tar.gz}
outtar="${out}.tar.gz"
outzip="${out}.zip"
echo "TARBALL: $outtar"
echo "ZIP: $outzip"

Using wget to download a tarball or zip file from GitHub

Use the wget

wget "$outtar"
wget "$outzip"

And the curl:

# The '-L' allow curl to follow redirects
# The '-O' allow to save and write output to a local file named like the remote file we get
curl -L -O "$outtar"
curl -L -O "$outzip"

# How to verify downloaded files from Github?
ls master*
file master*

And we have files:

master.tar.gz  master.zip
master.tar.gz: gzip compressed data, from Unix, original size modulo 2^32 901120
master.zip:    Zip archive data, at least v1.0 to extract
1 Like

Worked on my shell script <3


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki