Posted by & filed under Off topic, Software.

  • DZone
  • Reddit
  • HackerNews
  • Twitter
  • Facebook
  • Google Plus
  • Pinterest
  • StumbleUpon
  • LinkedIn
  • Tumblr
  • BlinkList
  • Mister Wong
  • Add to favorites
  • Email

tar-gzip Sometimes you need to extract some files on Linux console. Here  some examples how to do that.

tar

Tar archives are the most common way of distributing bundles of files under Linux or UNIX. A .tar file is simply a bundle of files packaged with GNU tar program. To extract such files use following:

tar xf somearchive.tar
tar xvf somearchive.tar
  • Provide option f if you want to extract content of files. Tar (from tape archive) has long history and was intended to work with tape media, so when you omit f tar tries to work with tape device.
  • v- stands for verbose. List all the files by extract process.
  • x- Extract command

Before extracting you may be interested in Looking inside of tar. Do it with option “t:

tar tf archive.tar

For more tar parameters see Man pages.

gzip

Often tar-files are also compressed. One of the most known compressed formats is GNU Zip (gzip).  Tar bundeld and zipped file would  normally  have extension .tar.gz. To extract such files you can use tar with  “z” option, which causes tar to automatically invoke gzip. Modify abow example  and you get able to extract tar.gz  files too.

tar -xzf somearchive.tar.gz

In old tar version the “z” option is may be not available. In that case just use UNIX pipes:

gzip -dc target.tar.gz | tar xf -

Meaning of gzip options

  • d – Do decompress!
  • c – write to console (So that tar can take it from there )
  • t – Tests file integrity
  • l – lists archive file information

You will find more on Man pages.

bzip2

Sometimes you can find files ending with .tar.bz2. That are files packaged with bzip (a block-sorting file compressor). Use it like gzip

tar xjvf filename.tar.bz2

Options d,c,t have the same meaning. More on Man pages.

zcat

Some files have .tar.Z endings. They can be extracted by

zcat somearchive.tar.Z | tar xf -

Look in Man pages for more.

Any questions? You are welcome to comment!

6 comments
shuron
shuron like.author.displayName like.author.displayName 2 Like

Hey Wesley, that is a really cool idea. I think i will use it. ;)

Wesley Rice
Wesley Rice like.author.displayName like.author.displayName 2 Like

I've defined the following function in my .bashrc to allow de-archiving just about any format with a simple 'unpack filename' command: function unpack () { case $1 in *.tar.bz2) tar xvjf $1 ;; *.tar.gz) tar xvzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar 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 "'$1' cannot be extracted via >unpack<" ;; esac }

shuron
shuron like.author.displayName 1 Like

Hey Moinul, i never done anything with Deluge, but found description of it . Maybe it helps you;)

Moinul Abrar
Moinul Abrar like.author.displayName 1 Like

Thank you for this hint. tar -xjf target.tar.bz2 How can I install "Deluge" torrent downloading software from "deluge-1.2.0_rc1.tar.bz2" which I download from Internet. Please help me. With thanks- Moinul Abrar

twam
twam

Your example of bzip2 is bzip2 -dc target.tar.gz | tar xf - I think the file should be named tar.bz2 as well. :) Modern versions of tar offer a direct use of bzip2 using the -j commandline switch: tar -xjf target.tar.bz2

shuron
shuron

Thank you for this hint. tar -xjf target.tar.bz2 should be added to the list

Trackbacks

  1. [...] Extracting files to the final location using tar. tar zxvf jboss-as-web-7.0.2.Final.tar.gz -C /usr/local/ Now your JBoss 7 is placed inside [...]

  2. [...] 31, 2011Looking on my block at the end of the year i see than nearly two years ago i wrote about extracting archives under Linux but not about putting files in to archives. Now a have some time to continue.Tar.gzHere [...]