Shortest way to download from GitHub
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
.
This will clone the files into new directory it creates:
git clone [email protected]:whatever NonExistentNewFolderName
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?