Skip to content

Instantly share code, notes, and snippets.

@shime

shime/_README.md

Last active Sep 2, 2020
Embed
What would you like to do?
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
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Nov 3, 2013

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

@dideler

This comment has been minimized.

Copy link

@dideler 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

This comment has been minimized.

Copy link
Owner Author

@shime 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

This comment has been minimized.

Copy link

@penguinpowernz penguinpowernz commented Nov 11, 2013

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

alias x="extract";
@Paxa

This comment has been minimized.

Copy link

@Paxa Paxa commented Feb 10, 2014

Thank you!

@dtigue

This comment has been minimized.

Copy link

@dtigue 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

This comment has been minimized.

Copy link

@witeshadow witeshadow commented May 21, 2016

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

@KaNaHaKEsUh

This comment has been minimized.

Copy link

@KaNaHaKEsUh KaNaHaKEsUh 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
You can’t perform that action at this time.