SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2009-01-13 10:58:10

Scrat
#! CrunchBanger
From: Denmark
Registered: 2009-01-10
Posts: 164

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 !

Offline

Be excellent to each other!

#2 2009-01-13 11:01:12

dannytatom
#! CrunchBanger
From: Ashdown, AR
Registered: 2009-01-10
Posts: 190

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

Offline

#3 2009-01-13 11:56:47

Nik_Doof
#! Junkie
Registered: 2008-12-13
Posts: 361
Website

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

Offline

#4 2009-01-14 07:20:23

NWAdawg
Bob Da Dawg!
From: Arkansas US
Registered: 2008-11-27
Posts: 192
Website

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

Offline

#5 2009-01-21 09:39:12

nitro-ale
Member
From: italia
Registered: 2009-01-15
Posts: 22

Re: HOWTO: Extract compressed files in terminals or console

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

Offline

#6 2009-01-21 14:24:13

michaelramm
The Inquisitor
From: Northport, AL USA
Registered: 2008-11-30
Posts: 175
Website

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!

Offline

#7 2009-01-21 14:26:58

papparonny
#! Junkie
From: norway
Registered: 2009-01-16
Posts: 272

Re: HOWTO: Extract compressed files in terminals or console

thank you scrat! big_smile

Offline

#8 2009-05-13 18:01:57

meravblum
New Member
Registered: 2009-05-13
Posts: 2

Re: HOWTO: Extract compressed files in terminals or console

how do i get fileroller?

Offline

#9 2009-05-13 18:18:04

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 9,418

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".

Offline

#10 2009-05-14 08:49:06

karthik
#! Member
Registered: 2009-01-29
Posts: 61

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)

Offline

#11 2009-05-25 19:41:28

karthik
#! Member
Registered: 2009-01-29
Posts: 61

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)

Offline

#12 2009-08-14 19:09:56

greener
Member
From: Norfolk, VA. USA
Registered: 2009-08-14
Posts: 15

Re: HOWTO: Extract compressed files in terminals or console

Pressed imaginary thank you button:)


Linux user #366129

Offline

#13 2009-08-14 19:20:21

Awebb
The Singularity
Registered: 2009-07-23
Posts: 2,812

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

Offline

#14 2010-03-22 19:59:25

guriinii
#! Member
From: UK
Registered: 2010-02-20
Posts: 74

Re: HOWTO: Extract compressed files in terminals or console

WOW! This is awesome.


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

Offline

#15 2012-02-05 21:52:52

lightning_underscore
Member
Registered: 2012-02-04
Posts: 13

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)

Offline

#16 2012-02-06 04:33:12

mahatman2
#! Die Hard
From: Chattanooga TN
Registered: 2009-09-13
Posts: 550

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.


Punch all your friends.

Offline

#17 2012-02-06 09:12:48

xaos52
The Good Doctor
From: Planet of the @s
Registered: 2011-06-24
Posts: 4,602

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.

Offline

#18 2012-02-06 11:29:29

greywolf
Member
From: Sydney, Australia
Registered: 2011-02-28
Posts: 39

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)


Life isn't about waiting for the storm to pass...
it's about learning to dance in the rain.

Offline

#19 2012-02-06 12:19:37

el_koraco
#!/loony/bun
From: inside Ed
Registered: 2011-07-25
Posts: 4,749

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.

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo