extract 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/bin/sh
  2. # A general, all-purpose extraction script.
  3. #
  4. # Default behavior: Extract archive into new directory
  5. # Behavior with `-c` option: Extract contents into current directory
  6. # Stolen from: https://github.com/LukeSmithxyz/voidrice/blob/master/.scripts/tools/extract
  7. while getopts "hc" o; do case "${o}" in
  8. c) extracthere="True" ;;
  9. *) printf "Options:\\n -c: Extract archive into current directory rather than a new one.\\n" && exit ;;
  10. esac done
  11. if [ -z "$extracthere" ]; then
  12. archive="$(readlink -f "$*")" &&
  13. directory=${archive%.*} &&
  14. mkdir -p "$directory" &&
  15. cd "$directory" || exit
  16. else
  17. archive="$(readlink -f "$(echo "$*" | cut -d' ' -f2)")"
  18. fi
  19. [ "$archive" = "" ] && printf "Give archive to extract as argument.\\n" && exit
  20. if [ -f "$archive" ] ; then
  21. case "$archive" in
  22. *.tar.bz2|*.tar.xz|*.tbz2) tar xvjf "$archive" ;;
  23. *.tar.gz|*.tgz) tar xvzf "$archive" ;;
  24. *.lzma) unlzma "$archive" ;;
  25. *.bz2) bunzip2 "$archive" ;;
  26. *.rar) unrar x -ad "$archive" ;;
  27. *.gz) gunzip "$archive" ;;
  28. *.tar) tar xvf "$archive" ;;
  29. *.zip) unzip "$archive" ;;
  30. *.Z) uncompress "$archive" ;;
  31. *.7z) 7z x "$archive" ;;
  32. *.xz) unxz "$archive" ;;
  33. *.exe) cabextract "$archive" ;;
  34. *) printf "extract: '%s' - unknown archive method\\n" "$archive" ;;
  35. esac
  36. else
  37. printf "File \"%s\" not found.\\n" "$archive"
  38. fi