Skip to content

Instantly share code, notes, and snippets.

@shime
Last active April 2, 2024 17:00
Show Gist options
  • 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
Copy link

ghost commented Nov 3, 2013

Nice one! One suggestion, you also see tbz, .tgz, txz around the web.

@dideler
Copy link

dideler commented Nov 5, 2013

Found this via coderwall. I have a similar function for the fish shell: https://github.com/dideler/dotfiles/blob/master/functions/extract.fish

@shime
Copy link
Author

shime commented Nov 5, 2013

Thanks for sharing, @dideler.

@MindTooth, thanks for the tip! I've updated the script and added a bunch of extensions and the error message.

@penguinpowernz
Copy link

Put this in your .bashrc/.bash_profile/.zshrc/.whateverrc to be able to extract using just x archive.tar.gz.

alias x="extract";

@Paxa
Copy link

Paxa commented Feb 10, 2014

Thank you!

@dtigue
Copy link

dtigue commented Apr 4, 2015

Sweet little script, not sure if you just didn't want to do it or what, but I am going to see if adding other compression types in to the script will work too. Would love for it to include unzip.

@witeshadow
Copy link

When I run it the file it says it is saving as is not the exact name the archive is saved as.

@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