Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save steinwaywhw/a4cd19cda655b8249d908261a62687f8 to your computer and use it in GitHub Desktop.
Save steinwaywhw/a4cd19cda655b8249d908261a62687f8 to your computer and use it in GitHub Desktop.
One Liner to Download the Latest Release from Github Repo
  • Use curl to get the JSON response for the latest release
  • Use grep to find the line containing file URL
  • Use cut and tr to extract the URL
  • Use wget to download it
curl -s https://api.github.com/repos/jgm/pandoc/releases/latest \
| grep "browser_download_url.*deb" \
| cut -d : -f 2,3 \
| tr -d \" \
| wget -qi -
@deltkastel
Copy link

deltkastel commented Feb 2, 2025

From batch scripts on Windows, you can call PowerShell without directly using its shell.:

First download grep.exe from this fairly popular but unmantained repo -because findstr sucks-

powershell iwr https://github.com/bmatzelle/gow/raw/refs/heads/master/bin/grep.exe -OutFile grep.exe

Then you can download your files like this.

set url=https://api.github.com/repos/user/repo/releases/latest
powershell irm %url% | grep -o 'https.*download.*zip' | powershell iwr -OutFile file.zip

Just replace user/repo and zip.

@Adamkaram
Copy link

for filtring and download something like beta release

curl -sL https://api.github.com/repos/telegramdesktop/tdesktop/releases
| jq -r '.[] | select(.prerelease==true) | .assets[] | select(.browser_download_url | contains("tar.xz")) | .browser_download_url'
| wget -i-

@dafanasiev
Copy link

for the latest version with a known file naming scheme:

PATCHELF_VERSION=$(curl -sLI https://github.com/NixOS/patchelf/releases/latest | grep -iP 'location.*tag' | grep -oP '\d+\.\d+\.\d+' | tail -1) \
&& curl -sL --output patchelf.tar.gz https://github.com/NixOS/patchelf/releases/download/${PATCHELF_VERSION}/patchelf-${PATCHELF_VERSION}-x86_64.tar.gz

@antofthy
Copy link

for the latest version with a known file naming scheme:

PATCHELF_VERSION=$(curl -sLI https://github.com/NixOS/patchelf/releases/latest | grep -iP 'location.*tag' | grep -oP '\d+\.\d+\.\d+' | tail -1) \
&& curl -sL --output patchelf.tar.gz https://github.com/NixOS/patchelf/releases/download/${PATCHELF_VERSION}/patchelf-${PATCHELF_VERSION}-x86_64.tar.gz

Removed the double grep, with a sed

PATCHELF_VERSION=$(curl -sLI https://github.com/NixOS/patchelf/releases/latest | sed '/location: .*\/tag\//!d; s///' | tail -1) &&
curl -sL --output patchelf.tar.gz https://github.com/NixOS/patchelf/releases/download/${PATCHELF_VERSION}/patchelf-${PATCHELF_VERSION}-x86_64.tar.gz

@dafanasiev
Copy link

for the latest version with a known file naming scheme:

PATCHELF_VERSION=$(curl -sLI https://github.com/NixOS/patchelf/releases/latest | grep -iP 'location.*tag' | grep -oP '\d+\.\d+\.\d+' | tail -1) \
&& curl -sL --output patchelf.tar.gz https://github.com/NixOS/patchelf/releases/download/${PATCHELF_VERSION}/patchelf-${PATCHELF_VERSION}-x86_64.tar.gz

Removed the double grep, with a sed

PATCHELF_VERSION=$(curl -sLI https://github.com/NixOS/patchelf/releases/latest | sed '/location: .*\/tag\//!d; s///' | tail -1) &&
curl -sL --output patchelf.tar.gz https://github.com/NixOS/patchelf/releases/download/${PATCHELF_VERSION}/patchelf-${PATCHELF_VERSION}-x86_64.tar.gz

++ case-insensitive location: match
++ remove regex escapes

PATCHELF_VERSION=$(curl -sLI https://github.com/NixOS/patchelf/releases/latest | sed '\|location:.*/tag/|I!d; s|||' | tail -1) \
&& curl -sL --output patchelf.tar.gz https://github.com/NixOS/patchelf/releases/download/${PATCHELF_VERSION}/patchelf-${PATCHELF_VERSION}-x86_64.tar.gz

@antofthy
Copy link

Actually instead of sed I could have just merge the two greps directly, using perl '\K' regex escape!

 PATCHELF_VERSION=$(curl -sLI https://github.com/NixOS/patchelf/releases/latest | grep -ioP 'location.*/tag/\K\d+\.\d+\.\d+' | tail -1) \
&& curl -sL --output patchelf.tar.gz https://github.com/NixOS/patchelf/releases/download/${PATCHELF_VERSION}/patchelf-${PATCHELF_VERSION}-x86_64.tar.gz
There are lots of ways to skin a cat, and what method you use depends
on what you want that skin for, and how messy you like the results!
                                                   -- Anthony Thyssen

@Mr-Bossman
Copy link

Not sure why everyone is using grep when sed can do everything.

curl -s https://api.github.com/repos/<user>/<repo>/releases/latest | sed -n 's/"browser_download_url": //p' | xargs wget

or

curl -s https://api.github.com/repos/<user>/<repo>/releases/latest | sed -n 's/"browser_download_url": "\(.*\)"$/\1/p' | wget -i-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment