Mastering tar on Linux
Tarball files are free software distributions that maintain a directory structure, source files, a Makefile, documentation and other files, encapsulated in a tar file with gzip data compression.
This software distribution method is very popular because the tar and gzip utilities are very common.
The result is a file with the .tar.gz or .tgz suffix.
You can still find tarball files with the .bz2 and .tbz2 suffixes. These files were compressed with bzip2, which uses a better data compression algorithm than gzip.
You can open the contents of a tarball file in two ways:
# gzip –d arquivo.tar.gz
The gzip command uncompresses arquivo.tar.gz and removes the .gz suffix.
# tar xvf arquivo.tar
The tar utility extracts the contents of the package.
We can also use simpler forms:
# tar xvzf arquivo.tar.gz
Or
# gzip –dc arquivo.tar.gz | tar xv
If the file is compressed with bzip2, it must be uncompressed by bunzip2 or use the bzip2 –d option.
# bzip2 –d arquivo.tar.bz2
Or
# bunzip2 arquivo.tar.bz2
And
# tar xvf arquivo.tar
See the comparison between compression performed by gzip and bzip2 compressors and an uncompressed tar file:
10.772.480 apache.tar 2.097.339 apache.tar.bz2 (5x menor) 2.467.371 apache.tar.gz (4x menor)
It is important to know that for Linux the file suffixes don't matter much. It checks the beginning of files to determine their format and which program is associated with that format.
Therefore, the suffixes .tar.gz, .tgz, .bz2 and .tbz2 are for human understanding.
The first bytes of files are called Magic Numbers, so each file format type has its own unique first bytes, which can be read to identify them.
The file utility can be used to read the first bytes of files and identify the file type.
Examples:
# file php-5.3.0.tar.gz
php-5.3.0.tar.gz: gzip compressed data, was "php-5.3.0.tar", from Unix, last modified: Mon Jun 29 12:46:48 2009, max compression
# file IntegrACAO.zip
IntegrACAO.zip: Zip archive data, at least v2.0 to extract
# file /bin/bash
/bin/bash: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.4, dynamically linked (uses shared libs), stripped
# file meutexto
meutexto: ASCII text
# file player.html
player.html: HTML document text
# file script
script: Bourne-Again shell script text
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Like it? Share it
That was just one piece. The course teaches the whole path — from your first command to exam day.