Skip to content
Alexander Holbreich
Go back

Unpacking archives with tar, gzip, bzip2, z

Sometimes there is a need to extract some files on the Linux console. Below you’ll find a list of the most common methods for that.

tar

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

tar xf somearchive.tar
tar xvf somearchive.tar
  • Provide option f if you want to extract the content of files. Tar (from tape archive) has a long history and was intended to work with tape media, so when you omit f tar try to work with a 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

gzip

Often tar files are also compressed. One of the most known compressed formats is GNU Zip (gzip). Tar bundled and zipped files would normally have extension .tar.gz. To extract such files you can use tar with z option. This way tar would also automatically invoke gzip. Modify the above example and you get able to extract tar.gz files too.

tar -xzf somearchive.tar.gz

In a very old tar version, the z flag was not available, initially, those operations were used with mighty UNIX pipes:

gzip -dc somearchive.tar.gz | tar xf -`

As we see in the above example. -d flag is used to decompress. Of course, you can decompress files without using tar. However, gzip is designed to compress (and replace uncompressed) files one by one (Find more details about it in the correspondingarticle), this rule is also applicable for decompression as we see above. Use k flag to keep the source file.

Gzip flags recap:

  • d - decompress
  • c - outputs to console instead of file
  • t - Tests file integrity
  • l - lists archive file information
  • v - Option shows affected file and compression ration
  • k - keeps source file

bzip2

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

tar xjvf filename.tar.bz2

Flags -d,-c,-t have the same meaning as in gzip above.

zcat

Zcat is a command line utility for viewing the contents of a compressed file without literally uncompressing it. It expands a compressed file to standard output allowing you to have a look at its contents. Actually, zcat works with gzipped files and extracts them to the console. So we can pipe and extract our tar.gz and tar.z files this way:

zcat somearchive.tar.z | tar xf -

The output to console is a powerfull Unix way to create desired outcomes

# Hehre searching inside the compressed files in one line without creating extra file
zcat example.docx.gz | grep "my function"

The output can be also redirected to a file wia > operator.

zcat file1.txt.gz file2.txt.gz > new_file.txt

zip

The most convenient way to work with zip files are zip and unzip programs. Both wou would be preinstalled on most Linux distributions.

#Unzip in place
unzip file.zip
#Unzip into the desired folder if the leaf directory do not exist it would be created (but only leaf)
unzip file.zip -d <destination_folder>
# Only list archive content
unzip -l file.zip

Custom ‘unpack’ Function

Do all this and more with one command, by adding a function to your .bashrc file. Thank you Wesley Rice.

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
}

See also Packaging files to archives with Linux


Archived comments (6)

These comments were migrated from Disqus and are no longer accepting replies.

  • 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

  • AlexH

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

  • Moinul Abrar

    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

  • AlexH

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

  • Wesley Rice

    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
    }

  • AlexH

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