Monday, October 06, 2008

Extract a tar.gz File in a Single Command [Unix]

The obvious way to extract a tar.gz archive is using the following two commands:
sharfah@starship:~> gunzip foo.tar.gz
sharfah@starship:~> tar xvf foo.tar
This is how you can achieve the same thing, but in a single command:
sharfah@starship:~> gunzip -c foo.tar.gz | tar xvf -
or alternatively:
sharfah@starship:~> gunzip < foo.tar.gz | tar xvf -
Both commands write the data produced from the gunzip command to standard out (-c in the first example and using < in the second). The output is then piped into the tar command. The "-" represents standard input.

Creating a tar.gz:
A tar.gz file is normally created using two commands as follows:

sharfah@starship:~> tar cvf foo.tar foodir
sharfah@starship:~> gzip foo.tar
Again, there is a single command to do the same thing:
sharfah@starship:~> tar cvf - foodir | gzip > foo.tar.gz

3 comments:

  1. Anonymous1:33 PM

    What about

    tar xvzf archive.tar.gz

    and

    tar cvzf archive.tar.gz files ...

    ReplyDelete
  2. On (K)Ubuntu I get away with simply:
    tar xfs archive.tar.gz

    ReplyDelete
  3. Nice article on tar command. For more examples on tar command, check this site: extract files from tar in unix

    ReplyDelete