Skip to content

Instantly share code, notes, and snippets.

@blockspacer
Forked from shime/_README.md
Last active June 5, 2020 08:01
Show Gist options
  • Save blockspacer/e44d4e3af5ef1ce498bb565cd94d4d0c to your computer and use it in GitHub Desktop.
Save blockspacer/e44d4e3af5ef1ce498bb565cd94d4d0c to your computer and use it in GitHub Desktop.
getting tired of guessing tar flags in 2013?

Looks familiar?

Worry no more! Use extract and prosper!

Usage

$ extract [FILES]

Installation

First method

Place it in your $PATH.

Second method

Do you trust me? You should, I'm a nice guy.

$ curl -fsSL https://gist.github.com/shime/5908634/raw/install | bash -e

If you get curl: (23) Failed writing body (0 != 462), it means you don't have write permissions to /usr/local/bin.

This will make it go away:

$ !!:s/bash/sudo bash

Zsh doesn't play really nice with that space, just paste this if it fails:

$ curl -fsSL https://gist.github.com/shime/5908634/raw/install | sudo bash -e

Enjoy!

#! /usr/bin/env bash
declare -A EXTENSION_DICT=( [tar.xz]="kxJf" [txz]="kxJf" [tar.bz2]="kxjf"
[tar.bz]="kxjf" [tar.gz]="kxzf"
[tgz]="kxzf" [tbz]="kxjf"
[tbz2]="kxjf" [tb2]="kxjf" )
function extract_archive(){
read file extension <<<$(echo $1 $2)
echo "extracting $file to ${file%$extension}"
eval "tar ${EXTENSION_DICT[$extension]} $file"
}
for file in $*
do
case "$file" in
*tar.xz)
extract_archive $file "tar.xz"
;;
*tar.bz2)
extract_archive $file "tar.bz2"
;;
*tar.bz)
extract_archive $file "tar.bz"
;;
*tar.gz)
extract_archive $file "tar.gz"
;;
*tgz)
extract_archive $file "tgz"
;;
*tbz)
extract_archive $file "tbz"
;;
*tbz2)
extract_archive $file "tbz2"
;;
*tb2)
extract_archive $file "tb2"
;;
*txz)
extract_archive $file "txz"
;;
*)
echo "Sorry, unrecognized file format."
exit 1
;;
esac
done
curl -fsSL https://gist.github.com/shime/5908634/raw/extract -o /usr/local/bin/extract
chmod +x /usr/local/bin/extract
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment