10

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

13
  • 21
    Why not clone the repository?
    – Kusalananda
    Commented Feb 4, 2018 at 13:14
  • 1
    Cloning the repository will download git information not needed by an end-user, even with the depth=1 option. Without the depth= option, the user could end up with a needlessly huge download. Commented Feb 4, 2018 at 13:28
  • 5
    @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
    Commented Feb 4, 2018 at 14:11
  • 2
    seems to be a popular question lately: one and two
    – Jeff Schaller
    Commented Feb 4, 2018 at 14:24
  • 2
    @JeffSchaller probably all one person. the other two questions definitely were.
    – cas
    Commented Feb 4, 2018 at 14:25

4 Answers 4

19

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.

4
  • 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? Commented Feb 4, 2018 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
    Commented Feb 4, 2018 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
    Commented Feb 5, 2018 at 6:25
  • @shorter For a custom .git/ folder location, see: stackoverflow.com/questions/5337025/…
    – Luc
    Commented Feb 5, 2018 at 13:53
4

This will clone the files into new directory it creates:

git clone [email protected]: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?

1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .