Skip to content

Instantly share code, notes, and snippets.

@shime
Last active April 2, 2024 17:00
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save shime/5908634 to your computer and use it in GitHub Desktop.
Save shime/5908634 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
@ConradQQ
Copy link

ConradQQ commented Sep 2, 2020

Very helpful! Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment