Skip to content

Instantly share code, notes, and snippets.

@MLKrisJohnson
Created March 24, 2023 19:54
Show Gist options
  • Save MLKrisJohnson/a220e601d1278b7065c0fb05d49a3d4a to your computer and use it in GitHub Desktop.
Save MLKrisJohnson/a220e601d1278b7065c0fb05d49a3d4a to your computer and use it in GitHub Desktop.
Bash/Zsh function to decompress a set of files into a subdirectory
# Unzip a set of files to an "unzipped" subdirectory
#
# Example:
#
# unzipped *.zip *.tar.gz
#
# will create the "unzipped" directory if not present, and decompress all the files into that directory.
function unzipped {
for f in "$@" ; do
directory=$(dirname "$f")
base=$(basename "$f")
filename=${base%.*}
mkdir -p "$directory/unzipped"
echo "$f"
if [[ $f = *.tar.gz ]]; then
filename=${filename%.*} # strip off the remaining ".tar" suffix
mkdir -p "$directory/unzipped/$filename"
/usr/bin/tar -zxv -C "$directory/unzipped/$filename" -f "$f"
else
/usr/bin/unzip -o -d "$directory/unzipped/$filename" "$f"
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment