29

I need to create a bash script that will work on a mac. It needs to download a ZIP file of a site and unzip it to a specific location.

  1. Download the ZIP file (curl -O)
  2. Unzip the files to a specific location (unzip filename.zip path/to/save)
  3. Delete the .zip file

I need to make it so people can double-click the text file on their desktop and it will automatically run in terminal.

How do I make it so that the user can double click the icon on the desktop and it will run? What extension does the file need?

2
  • 2
    The file needs a .cmd extension. Commented Dec 19, 2011 at 5:12
  • curl -L http://example.org/file.zip | bsdtar -xvf - -C /path/to/save Commented Mar 16, 2017 at 15:47

3 Answers 3

42

OSX uses the same GNU sh/bash as Linux

#!/bin/sh

mkdir /tmp/some_tmp_dir                         && \
cd /tmp/some_tmp_dir                            && \
curl -sS http://foo.bar/filename.zip > file.zip && \
unzip file.zip                                  && \
rm file.zip

the first line #!/bin/sh is so called "shebang" line and is mandatory

5
  • 2
    By default wget isn't installed in Mac OS. curl is. Commented Dec 19, 2011 at 5:08
  • 5
    Is it possible to pipe curl result directly to unzip without creating file.zip?
    – pogibas
    Commented May 9, 2013 at 12:34
  • if you prefer to see the progress dont use the silence -sS flag
    – Aras
    Commented Mar 3, 2014 at 22:04
  • add set -e at the beginning of the file and --fail to the curl command if you want the script to exit if the fie couldn't be downloaded.
    – Joscha
    Commented Dec 2, 2015 at 1:26
  • For Bash it is sufficient to terminate the line with &&. The backslashes are not required.
    – ceving
    Commented Apr 18, 2019 at 13:37
16

BSD Tar can open a zip file and decompress through a stream.The -L or --location flag is to follow redirects. So the following will work:

curl --show-error --location http://example.org/file.zip | tar -xf - -C path/to/save
6
  • 8
    The file has to be a tar file otherwise tar: This does not look like a tar archive Commented Nov 9, 2015 at 17:52
  • 1
    it works for tar on Mac, but gives error on Linux
    – Slavik
    Commented Apr 17, 2019 at 19:08
  • 1
    I just realised that duplicated this answer. It works on mac and linux for me. You might be having a redirect and not following it the -L param. try curl -SL https://www.sample-videos.com/zip/10mb.zip | tar -xz - -C .
    – lauksas
    Commented Apr 18, 2019 at 13:57
  • can you read? this is about ZIP files, not TAR files
    – iirekm
    Commented Jan 7, 2023 at 15:12
  • @iirekm and can you read that "BSD Tar" (which uses libarchive) can open a zip file. ;)
    – ruario
    Commented Jan 29, 2023 at 22:13
8

If you do not want change directory context, use the following script:

#!/bin/bash

unzip-from-link() {
 local download_link=$1; shift || return 1
 local temporary_dir

 temporary_dir=$(mktemp -d) \
 && curl -LO "${download_link:-}" \
 && unzip -d "$temporary_dir" \*.zip \
 && rm -rf \*.zip \
 && mv "$temporary_dir"/* ${1:-"$HOME/Downloads"} \
 && rm -rf $temporary_dir
}

Usage:

# Either launch a new terminal and copy `git-remote-url` into the current shell process, 
# or create a shell script and add it to the PATH to enable command invocation with bash.

# Place zip contents into '~/Downloads' folder (default)
unzip-from-link "http://example.com/file.zip"

# Specify target directory
unzip-from-link "http://example.com/file.zip" "/your/path/here"

Output:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 17.8M  100 17.8M    0     0  22.6M      0 --:--:-- --:--:-- --:--:-- 22.6M
Archive:  file.zip
  inflating: /tmp/tmp.R5KFNvgYxr/binary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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