Created
May 3, 2019 19:37
-
-
Save cmccandless/6dc1ee1ee34bb64e6af795b4d8f66b94 to your computer and use it in GitHub Desktop.
Tarball Tree script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
DEFAULT_MAX_DEPTH=4 | |
DEFAULT_OUTPUT_PATH=./update | |
print_usage() | |
{ | |
echo "$0 [options] TARFILE" | |
echo "options:" | |
echo " -h,--help print this message" | |
echo " -k,--keep do not remove temporary output directory" | |
echo " -m DEPTH,--max-depth DEPTH maxiumum recursion depth (default: $DEFAULT_MAX_DEPTH)" | |
echo " -d DIR,--direcotry DIR temporary output directory path (default: $DEFAULT_OUTPUT_PATH)" | |
} | |
extract() | |
{ | |
tarfile="$1" | |
directory="${2:-${tarfile}.d}" | |
maxdepth="${3:-$DEFAULT_MAX_DEPTH}" | |
mkdir "$directory" | |
tar -xf "$tarfile" -C "$directory" | |
if [ "$maxdepth" -gt 1 ]; then | |
cd "$directory" | |
for nested_tarfile in $(ls ./*.tgz 2> /dev/null); do | |
extract "$nested_tarfile" "${nested_tarfile}.d" $(( maxdepth - 1 )) | |
done | |
cd .. | |
fi | |
} | |
MAXDEPTH="$DEFAULT_MAX_DEPTH" | |
KEEP=0 | |
DIRECTORY="$DEFAULT_OUTPUT_PATH" | |
POSITIONAL=() | |
while [[ $# -gt 0 ]]; do | |
arg="$1" | |
case "$arg" in | |
-h|--help) | |
print_usage | |
exit 1 | |
;; | |
-k|--keep) | |
KEEP=1 | |
shift | |
;; | |
-m|--max-depth) | |
shift | |
MAXDEPTH="$1" | |
shift | |
;; | |
-d|--directory) | |
shift | |
DIRECTORY="$1" | |
shift | |
;; | |
*) | |
POSITIONAL+=("$1") | |
shift | |
;; | |
esac | |
done | |
set -- "${POSITIONAL[@]}" | |
TARFILE="$1" | |
extract "$TARFILE" "$DIRECTORY" "$MAXDEPTH" | |
tree "$DIRECTORY" | |
if [ "$KEEP" -eq 0 ]; then | |
rm -rf update | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment