How do I write a BASH script to download and unzip file on a Mac?
If you do not want change directory context, use the following script:
#!/bin/bashunzip-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 directoryunzip-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 Speed100 17.8M 100 17.8M 0 0 22.6M 0 --:--:-- --:--:-- --:--:-- 22.6MArchive: file.zip inflating: /tmp/tmp.R5KFNvgYxr/binary