Skip to content

Instantly share code, notes, and snippets.

@soraxas
Last active September 1, 2023 05:03
Show Gist options
  • Save soraxas/ab594b0cf5c49b4e36c04249f0b7c2f7 to your computer and use it in GitHub Desktop.
Save soraxas/ab594b0cf5c49b4e36c04249f0b7c2f7 to your computer and use it in GitHub Desktop.
script:Auto extraction tool based on the file extension
#!/bin/bash
SAVEIFS=$IFS
IFS="$(printf '\n\t')"
DEFAULT_FORMAT=tar.gz
SCRIPT="$(basename $0)"
function extract {
if [ -z "$1" ]; then
# display usage if no parameters given
echo "Usage: $SCRIPT <target.(zip|rar|tar.*|7z)> <file1> [file2] ..."
echo "Usage: $SCRIPT <file1>"
echo ""
echo "Use corresponding compression software based on output filename"
echo "If only one input is given, .$DEFAULT_FORMAT format is assumed, and named as file1.$DEFAULT_FORMAT"
else
target="$1"
#if [ -f "$target" ] ; then
# echo "'$target' - file already exist!"
# return 1
#fi
if [ -z "$2" ]; then
# echo "Require at least one file to compress!"
# return 1
# remove trailing slash
target="${target%/}".$DEFAULT_FORMAT
else
shift
fi
case "${target%,}" in
*.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
tar cavf "$target" $@
;;
# *.lzma) unlzma ./"$target" ;;
# *.bz2) bunzip2 ./"$target" ;;
# *.cbr|*.rar) unrar x -ad ./"$target" ;;
# *.gz) gunzip ./"$target" ;;
# *.cbz|*.epub|*.zip)
# unzip ./"$target" $unzip_args
# ;;
# *.z) uncompress ./"$target" ;;
*.7z)
7z a "$target" $@
;;
*.zip)
zip -r "$target" $@
;;
*.rar)
rar a "$target" $@
;;
# *.7z|*.apk|*.arj|*.cab|*.cb7|*.chm|*.deb|*.dmg|*.iso|*.lzh|*.msi|*.pkg|*.rpm|*.udf|*.wim|*.xar)
# 7z x ./"$target" ;;
# *.xz) unxz ./"$target" ;;
# *.exe) cabextract ./"$target" ;;
# *.cpio) cpio -id < ./"$target" ;;
# *.cba|*.ace) unace x ./"$target" ;;
# *.zpaq) zpaq x ./"$target" ;;
# *.arc) arc e ./"$target" ;;
# *.cso) ciso 0 ./"$target" ./"$target.iso" && \
# extract $target.iso && \rm -f $target ;;
*)
echo "$SCRIPT: '$target' - unknown archive method"
return 1
;;
esac
fi
}
IFS=$SAVEIFS
extract $@
#!/bin/bash
# from https://github.com/xvoland/Extract
# function Extract for common file format
SAVEIFS=$IFS
IFS="$(printf '\n\t')"
script="$(basename $0)"
function extract {
if [ -z "$1" ]; then
# display usage if no parameters given
echo "Usage: $script <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
echo " $script <path/file_name_1.ext> [destination_directory]"
echo ""
echo "Note that destination_directory is only implemented on some formats."
else
file="$1"
if [ ! -f "$file" ] ; then
echo "'$file' - file does not exist"
return 1
fi
if [ -n "$2" ]; then
mkdir -p "$2" || return $?
tar_args=--directory="$2"
unzip_args="-d $2"
fi
case "${file%,}" in
*.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar)
tar xvf "$file" $tar_args
;;
*.lzma) unlzma ./"$file" ;;
*.bz2) bunzip2 ./"$file" ;;
*.cbr|*.rar) unrar x -ad ./"$file" ;;
*.gz) gunzip ./"$file" ;;
*.cbz|*.epub|*.zip)
unzip ./"$file" $unzip_args
;;
*.z) uncompress ./"$file" ;;
*.7z|*.apk|*.arj|*.cab|*.cb7|*.chm|*.deb|*.dmg|*.iso|*.lzh|*.msi|*.pkg|*.rpm|*.udf|*.wim|*.xar)
7z x ./"$file" ;;
*.xz) unxz ./"$file" ;;
*.exe) cabextract ./"$file" ;;
*.cpio) cpio -id < ./"$file" ;;
*.cba|*.ace) unace x ./"$file" ;;
*.zpaq) zpaq x ./"$file" ;;
*.arc) arc e ./"$file" ;;
*.cso) ciso 0 ./"$file" ./"$file.iso" && \
extract "$file.iso" && \rm -f "$file" ;;
*)
echo "$script: '$file' - unknown archive method"
return 1
;;
esac
fi
}
IFS=$SAVEIFS
extract "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment