Skip to content

Instantly share code, notes, and snippets.

@maddouri
Last active February 25, 2018 16:20
Show Gist options
  • Save maddouri/70e14dcd25ffecf2226204edd14315cc to your computer and use it in GitHub Desktop.
Save maddouri/70e14dcd25ffecf2226204edd14315cc to your computer and use it in GitHub Desktop.
Downloading and Decompressing an Archive in One Command
#!/usr/bin/env bash
set -eu
## untar_url <archive_url> <dest_dir> [<nb_components_to_strip>]
##
## Downloads an archive from <archive_url> and decompresses it in <dest_dir>
##
## @param archive_url archive url
## @param dest_dir directory in which to decompress the archive (created if not existing)
## @param nb_components_to_strip number of components to strip from file names on extraction. Default=1
##
## @see https://general-purpose.io/2017/02/09/downloading-and-decompressing-an-archive-in-one-command/
function untar_url()
{
if [ "$#" != "2" -a "$#" != "3" ] ; then
>&2 echo "Wrong argument count"
>&2 echo "Usage: ${FUNCNAME[0]} <archive_url> <dest_dir> [<nb_components_to_strip>]"
return 1
fi
local archive_url="$1"
local dest_dir="$2"
local nb_components_to_strip="${3-1}"
local file_size="$(wget --spider "${archive_url}" 2>&1 | grep Length | awk '{print $2}')"
mkdir -p "${dest_dir}"
wget -qO- "${archive_url}" | pv -s "${file_size}" | tar xzf - -C "${dest_dir}" --strip-components "${nb_components_to_strip}"
}
untar_url "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment