Download executable script from GitHub preserving +x permissions

7,397

Solution 1

I would suggest you use gists instead of a git repository. Repository by nature is supposed to be a collection of files. Gists on the other hand are single files. Something like this

wget https://gist.githubusercontent.com/SergKolo/d77dd46e9d936b5871e1829a2afd79c3/raw/726f39be5e0a939e06ed89239d2c350451b81316/get_unity_viewports.sh \
&& chmod +x ./get_unity_viewports.sh

But if you insist on using a repository , instead of 1 command , combine 2 into 1. Rememeber that we can specify interpreter for the code.

curl https://raw.githubusercontent.com/au-answers/dupe-check/master/dupe-check  > dupe-check && python3 dupe-check

And that's how it runs

$ curl https://raw.githubusercontent.com/au-answers/dupe-check/master/dupe-check\                                          
>   > dupe-check && python3 dupe-check
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7580  100  7580    0     0   6484      0  0:00:01  0:00:01 --:--:--  6489
Checked 1 files in total, 0 of them are duplicates by content.

Same with wget

$ wget https://raw.githubusercontent.com/au-answers/dupe-check/master/dupe-check && python3 dupe-check                     
--2016-04-24 21:17:35--  https://raw.githubusercontent.com/au-answers/dupe-check/master/dupe-check
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 23.235.44.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|23.235.44.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 7580 (7.4K) [text/plain]
Saving to: ‘dupe-check.1’

100%[==================================================================================>] 7,580       --.-K/s   in 0s      

2016-04-24 21:17:36 (470 MB/s) - ‘dupe-check.1’ saved [7580/7580]

Checked 2 files in total, 2 of them are duplicates by content.
Here's a list of all duplicate files:

'dupe-check' (./dupe-check)
'dupe-check.1' (./dupe-check.1)

Variation on the theme

Get the URL of raw file by right clicking on the raw button in github

wget https://github.com/au-answers/dupe-check/raw/master/dupe-check

Solution 2

Get the tarballs!

Even though Github doesn't expose this in the site (apparently they did ages ago), they provide tar.gz files of the repositories.

wget -qO - https://github.com/<user>/repo>/archive/master.tar.gz | tar zx --strip-components=1 <repo>-master/<filename>

<user>, <repo>, <filename> are what you'd expect them to be. --strip-components is use to prevent tar from creating a directory named after the repo.

Therefore:

$ wget -O - https://github.com/au-answers/dupe-check/archive/master.tar.gz | tar zx --strip-components=1 dupe-check-master/dupe-check
--2016-04-25 08:28:50--  https://github.com/au-answers/dupe-check/archive/master.tar.gz
Resolving github.com (github.com)... 192.30.252.128
Connecting to github.com (github.com)|192.30.252.128|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/au-answers/dupe-check/tar.gz/master [following]
--2016-04-25 08:28:51--  https://codeload.github.com/au-answers/dupe-check/tar.gz/master
Resolving codeload.github.com (codeload.github.com)... 192.30.252.160
Connecting to codeload.github.com (codeload.github.com)|192.30.252.160|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2470 (2.4K) [application/x-gzip]
Saving to: ‘STDOUT’

-                                                           100%[========================================================================================================================================>]   2.41K  --.-KB/s    in 0s      

2016-04-25 08:28:52 (21.5 MB/s) - written to stdout [2470/2470]

$ ll dupe-check 
-rwxr-xr-x 1 muru muru 7.5K Mar  5 21:46 dupe-check

I'm not convinced this is a particularly friendly way. An additional chmod +x is IMO infinitely preferable.

Failed attempts

Get the zips!

You can process substitution on zsh to extract the zips linked on the Github repo page. Github, thankfully, adds extra metadata to zips so that permissions are preserved. unzip on Ubuntu is able to use that to restore permissions. So:

unzip -j =(wget -O - https://github.com/au-answers/dupe-check/archive/master.zip) dupe-check-master/dupe-check

Unfortunately, =() is a zsh thing. It creates a proper file instead of a FIFO. Bash doesn't have an equivalent. <() is always FIFO.

Git single files

You can get a single file from repo using git. However, Github doesn't support this.

Share:
7,397

Related videos on Youtube

Byte Commander
Author by

Byte Commander

Ask Ubuntu moderator♦, IT student and DevOps engineer. I love Ubuntu, Python, good music and coffee, although not necessarily in that order. You can easily contact me in the Ask Ubuntu General Room most of the time, or on Discord as @ByteCommander#2800. I'd also love to invite you to my Ubuntu Hideout Discord Server btw. PS: My profile picture is derived from "Wolf Tribals" by user HaskDitex (DeviantArt.com), which is under creative Commons 3.0 License. Currently I'm using the character "Dregg Morriss" from the game "Medieval Cop" by Vasant Jahav ("Gemini Gamer"). It can be found here.

Updated on September 18, 2022

Comments

  • Byte Commander
    Byte Commander over 1 year

    I want to make a GitHub repository of all relevant scripts I posted here as answers.

    But to keep it ultimately simple for the users, I would like to give them a single command to download the script file from my repository directly, without having to install git first to git clone the entire repository.

    I know I can use wget to download a single file, but when I use the "Raw" link of a file (e.g. https://github.com/AskUbuntu-Answers/dupe-check/raw/master/dupe-check), the file will get default umask permissions and therefore has no execution bit set. But I don't want that the user still has to run chmod +x.

    The script files are committed pushed to the repository with correct execution bits, they are also preserved when I use git clone to get the entire repository.

    What is the simplest way to retrieve (and sometimes automatically execute) a single file from GitHub preserving its executing permission without having to install git and cloning the entire repository?

    • kos
      kos about 8 years
      Automatically setting the executable permission without making the user somehow run chmod / change their umask I'd say would be impossible. GitHub AFAIK doesn't support anything other than HTTPS, so I'd say using a protocol which allows to keep permissions would be impossible either. Executing the file automatically on the other hand would be easy, something like wget -O - https://raw.githubusercontent.com/AskUbuntu-Answers/dupe-che‌​ck/master/dupe-check | env python3.
    • muru
      muru about 8 years
      @kos Ewww. Something like python3 <(wget -O - https://raw.githubusercontent.com/AskUbuntu-Answers/dupe-che‌​ck/master/dupe-check‌​) would be better, if only to prevent any accidental use of stdin from chewing up the script.
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 8 years
    Almost the same thing