7

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?).

  • 21
    Why not clone the repository? – Kusalananda Feb 4 '18 at 13:14
  • 4
    @user1404316 yeah, because "end users" never need to run "git pull" because they're never interested in compiling the latest version, or running git show or git log to see what's changed between versions. – cas Feb 4 '18 at 14:11
  • 2
    seems to be a popular question lately: one and two – Jeff Schaller Feb 4 '18 at 14:24
  • 2
    @JeffSchaller probably all one person. the other two questions definitely were. – cas Feb 4 '18 at 14:25
  • 1
    unzip should take an argument for a directory to extract into, so you could create the parent dir first – Will Crawford Feb 4 '18 at 15:04
14

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.

  • Thanks! Small question. I noticed that besides downloading the repo, it also created a dir named .git with many files in it, is there a necessity to create this dir? Is there a way to avoid creating it? – shorter Feb 4 '18 at 18:50
  • 2
    @shorter, there shouldn't be too many files in .git, and no, you can't do without it. What you can do though is remove all of the other files after compiling, then reconstitute them later if needed with git checkout. As an added bonus, when you want to get the latest version in the future, a git pull will only need to download a small amount of data to represent what has changed, instead of downloading the whole thing again. – psusi Feb 4 '18 at 19:09
  • @shorter As long as you are using git, I don't know of any way to avoid creating the .git directory, but you can safely delete it after the command has completed. I can't think of any reason creating it would be a problem, though. Anyway, if you do this you lose the ability to easily update to a future revision. – David Z Feb 5 '18 at 6:25
  • @shorter For a custom .git/ folder location, see: stackoverflow.com/questions/5337025/… – Luc Feb 5 '18 at 13:53
4

This will clone the files into new directory it creates:

git clone git@github.com:whatever NonExistentNewFolderName
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 "%s\n" "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 "%s\n" "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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.