Topic: HOWTO: Extract compressed files in terminals or console

I know that this is easily done using Fileroller or similar GUI applications.

But I like to do most of my file operations using Terminals, because it's faster and I always gets the appropriate outputs if something goes wrong.

This tip is from the Arch Wiki and is one of my favourites smile

Open ~/.bashrc in your preferred editor and paste the following in the bottom of this file:

extract () {
  if [ -f $1 ] ; then
      case $1 in
          *.tar.bz2)   tar xvjf $1    ;;
          *.tar.gz)    tar xvzf $1    ;;
          *.bz2)       bunzip2 $1     ;;
          *.rar)       rar x $1       ;;
          *.gz)        gunzip $1      ;;
          *.tar)       tar xvf $1     ;;
          *.tbz2)      tar xvjf $1    ;;
          *.tgz)       tar xvzf $1    ;;
          *.zip)       unzip $1       ;;
          *.Z)         uncompress $1  ;;
          *.7z)        7z x $1        ;;
          *)           echo "don't know how to extract '$1'..." ;;
      esac
  else
      echo "'$1' is not a valid file!"
  fi
}

For making this to take effect immediately type this command in your preferred terminal:

source .bashrc

Find a compressed file ie. (foo.tar.gz) and type this:

extract foo.tar.gz

Voila big_smile

Last edited by Scrat (2009-01-13 11:01:27)

/Niels
Registered Linux user #133791 > Get counted at http://counter.li.org
-----------------------------------------------------------------------
So Many Things To Do, And No Time For Nuts !

Re: HOWTO: Extract compressed files in terminals or console

Awesome tip, much thanks!  We really do need a thank you button. tongue

.files
dnyy in IRC & Urban Terror

Re: HOWTO: Extract compressed files in terminals or console

Might want to add tar.Z as well, rare, but still occur in a few places smile

Re: HOWTO: Extract compressed files in terminals or console

dannytatom wrote:

Awesome tip, much thanks!  We really do need a thank you button. tongue

I got to second that. Great tip.

-Bob- 
Ubuntu User #24005, Linux User #480025
identi.ca  friendfeed  facebook

Re: HOWTO: Extract compressed files in terminals or console

great script!
i can't remember everytime tar -xvfsjsxsdtxxx.... big_smile

Re: HOWTO: Extract compressed files in terminals or console

AWESOME! I can never remember all of those switches to extract things.

I guess I will never learn them now, and even be upset when I am on another system and type extract and it bonks at me!

Thanks for that addition.
Michael

The 1-Man IT Department | Ubuntu User #16666 | Linux User #451972
My Social Nets: Identi.ca | twitter | friendfeed
Crunchbangin' and Loving Every Minute of IT!

Re: HOWTO: Extract compressed files in terminals or console

thank you scrat! big_smile

Re: HOWTO: Extract compressed files in terminals or console

how do i get fileroller?

Re: HOWTO: Extract compressed files in terminals or console

It should already be included in #!. If you don't have it, you can install it by running "sudo apt-get install file-roller".

Note: ** Please read before posting **

BTW if you wish to contact me, send me an e-mail instead of a PM.

Re: HOWTO: Extract compressed files in terminals or console

Thanks Scrat, this is a useful tip.

Last edited by karthik (2009-05-14 08:49:52)

Re: HOWTO: Extract compressed files in terminals or console

An improvement, for when dealing with several archives:

extract () {
    for archive in $*; do
        if [ -f $archive ] ; then
            case $archive in
                *.tar.bz2)   tar xvjf $archive    ;;
                *.tar.gz)    tar xvzf $archive    ;;
                *.bz2)       bunzip2 $archive     ;;
                *.rar)       rar x $archive       ;;
                *.gz)        gunzip $archive      ;;
                *.tar)       tar xvf $archive     ;;
                *.tbz2)      tar xvjf $archive    ;;
                *.tgz)       tar xvzf $archive    ;;
                *.zip)       unzip $archive       ;;
                *.Z)         uncompress $archive  ;;
                *.7z)        7z x $archive        ;;
                *)           echo "don't know how to extract '$archive'..." ;;
            esac
        else
            echo "'$archive' is not a valid file!"
        fi
    done
}

Can be used like so:
$ extract *.tar.gz *.zip

Last edited by karthik (2009-05-25 19:42:00)

Re: HOWTO: Extract compressed files in terminals or console

Pressed imaginary thank you button:)

Linux user #366129

Re: HOWTO: Extract compressed files in terminals or console

And the next level will be having the script identify the file by its mime-type big_smile

It seems that every community is unique for some things. I read alot of links to #! when it comes to conky and Arch is a script factory par exellence big_smile

I'm so meta, even this acronym

Re: HOWTO: Extract compressed files in terminals or console

WOW! This is awesome.

#!Crunchbang - an inverted wall of learning, definitely no curve!

Re: HOWTO: Extract compressed files in terminals or console

roll Very nice. This is enlightenment... it's almost as if I understand computers now.... Nope, not yet.
haha Very helpful.

Question to anyone who reads this... can this be applied system wide? I only have it in my homefolder where the script will work.

Thefefore I must:

 cp /folder/file.gz /home/USER/ 
 cd /home/USER/ 
 extract file.gz 

I like using this for man files and the like... not all of them come pre-extracted.

Last edited by lightning_underscore (2012-02-05 22:13:48)

Re: HOWTO: Extract compressed files in terminals or console

just put it in /usr/bin or somewhere in $PATH, then it'll work from wherever.

Love is fleeting
Kittens R Forever

Re: HOWTO: Extract compressed files in terminals or console

Put the function definition in your $HOME/.bashrc (append it to the end of .bashrc)
Log out and log in again
You then will be able to use it everywhere.

Re: HOWTO: Extract compressed files in terminals or console

Can I just offer a small variation to this great tip please. There is actually a 'built-in' command in Linux called 'extract'. At any point in time you may or may not have the util package installed that includes it; but, if it is installed it will conflict with the command from this tip. That can cause the system to give unexpected results.

I have been using this for years in all sorts of distros and I have found the easiest way to avoid the conflict arising is to change the name of this tip command to just "extr" rather than the full word extract. Here is the relevant section from my .bashrc file:

# universal tarball extractor
    extr () {
        if [ -f $1 ] ; then
            case $1 in
                *.tar.bz2)   tar xjf $1        ;;
                *.tar.gz)    tar xzf $1     ;;
                *.bz2)       bunzip2 $1       ;;
                *.rar)       rar x $1     ;;
                *.gz)        gunzip $1     ;;
                *.tar)       tar xf $1        ;;
                *.tbz2)      tar xjf $1      ;;
                *.tgz)       tar xzf $1       ;;
                *.zip)       unzip $1     ;;
                *.Z)         uncompress $1  ;;
                *.7z)        7z x $1    ;;
                *)           echo "'$1' cannot be extracted via extr()" ;;
            esac
        else
            echo "'$1' is not a valid file"
        fi
    }

Also, make sure you have all of the programs installed that actually do the extractions.
Works really well.

cheers,
Greywolf.

Last edited by greywolf (2012-02-06 11:32:18)

Re: HOWTO: Extract compressed files in terminals or console

There's a package in the Debian repos, called unp. It's a wrapper around tar, unzip and gang. You just do unp archive.