Created
July 20, 2022 15:01
-
-
Save wasertech/abf49b3fe6a5a17ff82d767c83abf568 to your computer and use it in GitHub Desktop.
Zsh function to extract common file formats
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
### Function extract for common file formats ### | |
SAVEIFS=$IFS | |
IFS=$(echo -en "\n\b") | |
function extract { | |
if [ -z "$1" ]; then | |
# display usage if no parameters given | |
echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>" | |
echo " extract <path/file_name_1.ext> [path/file_name_2.ext] [path/file_name_3.ext]" | |
else | |
for n in "$@" | |
do | |
if [ -f "$n" ] ; then | |
case "${n%,}" in | |
*.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar) | |
tar xvf "$n" ;; | |
*.lzma) unlzma ./"$n" ;; | |
*.bz2) bunzip2 ./"$n" ;; | |
*.cbr|*.rar) unrar x -ad ./"$n" ;; | |
*.gz) gunzip ./"$n" ;; | |
*.cbz|*.epub|*.zip) unzip ./"$n" ;; | |
*.z) uncompress ./"$n" ;; | |
*.7z|*.arj|*.cab|*.cb7|*.chm|*.deb|*.dmg|*.iso|*.lzh|*.msi|*.pkg|*.rpm|*.udf|*.wim|*.xar) | |
7z x ./"$n" ;; | |
*.xz) unxz ./"$n" ;; | |
*.exe) cabextract ./"$n" ;; | |
*.cpio) cpio -id < ./"$n" ;; | |
*.cba|*.ace) unace x ./"$n" ;; | |
*) | |
echo "extract: '$n' - unknown archive method" | |
return 1 | |
;; | |
esac | |
else | |
print_center_with_banner "'$n' - file does not exist" | |
return 1 | |
fi | |
done | |
fi | |
} | |
IFS=$SAVEIFS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's written for
zsh
but should work withbash
too.You need the following tools to use (fully):
tar
unlzma
bunzip2
unrar
gunzip
unzip
uncompress
7z
unxz
cabextract
cpio
unace
Example: