Shortest way to download from GitHub

This is, how I download various master branches from GitHub, and I aim to have a prettier script (and maybe more reliable?).

wget -P ~/ https://github.com/user/repository/archive/master.zip
unzip ~/master.zip
mv ~/*-master ~/dir-name

Can this be shorten to one line somehow, maybe with tar and pipe?

Please address issues of downloading directly to the home directory ~/ and having a certain name for the directory (mv really needed?).

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The shortest way that seems to be what you want would be git clone https://github.com/user/repository --depth 1 --branch=master ~/dir-name. This will only copy the master branch, it will copy as little extra information as possible, and it will store it in ~/dir-name.

Method 2

This will clone the files into new directory it creates:

git clone <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a4aab783a4aab7abb6a1eda0acae">[email protected]</a>:whatever NonExistentNewFolderName

Method 3

Let’s start with the bash function I had handy for my own personal use:

wget_github_zip() {
  if [[ $1 =~ ^-+h(elp)?$ ]] ; then
    printf "%sn" "Downloads a github snapshot of a master branch.nSupports input URLs of the forms */repo_name, *.git, and */master.zip"
    return
    fi
  if [[ ${1} =~ /archive/master.zip$ ]] ; then
    download=${1}
    out_file=${1//archive/master.zip}
    out_file=${out_file##*/}.zip
  elif [[ ${1} =~ .git$ ]] ; then
    out_file=${1/%.git}
    download=${out_file}/archive/master.zip
    out_file=${out_file##*/}.zip
  else
    out_file=${1/%/} # remove trailing '/'
    download=${out_file}/archive/master.zip
    out_file=${out_file##*/}.zip
    fi
  wget -c ${download} -O ${out_file}
  }

You want the file to always be called master.zip and always be downloaded into your home directory, so:

wget_github_zip() {
  if [[ $1 =~ ^-+h(elp)?$ ]] ; then
    printf "%sn" "Downloads a github snapshot of a master branch.nSupports input URLs of the forms */repo_name, *.git, and */master.zip"
    return
    fi
  if [[ ${1} =~ /archive/master.zip$ ]] ; then
    download=${1}
  elif [[ ${1} =~ .git$ ]] ; then
    out_file=${1/%.git}
    download=${out_file}/archive/master.zip
  else
    out_file=${1/%/} # remove trailing '/'
    download=${out_file}/archive/master.zip
    fi
  wget -c ${download} -O ~/master.zip && unzip ~/master.zip && mv ~/master.zip ~/myAddons
  }

However, there are a few things for you to consider:

1) My original script will give you a unique download zip file name for each download, based upon the name of the github repository, which is generally what most people really want instead of everything being called master and having to manually rename them afterwards for uniqueness. In that version of the script, you can use the value of $out_file to uniquely name the root directory unzipped tree.

2) If you really do want all downloaded zip files to be named ~/master.zip, do you want to delete each after they have been unzipped?

3) Since you seem to always want everything put in directory ~/myAddons, why not perform all the operations there, and save yourself the need to move the unzipped directory?

Method 4

Here’re few ways to download + unzip in oneliner command:

# With WGET and JAR:
wget -O - https://github.com/user/repo/archive/master.zip | jar xv

# With WGET and TAR:
wget -O - https://github.com/user/repo/archive/master.tar.gz | tar xz

# With CURL and JAR:
curl -L https://github.com/user/repo/archive/master.zip | jar xv

# With CURL and TAR:
curl -L https://github.com/user/repo/archive/master.tar.gz | tar xz


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x