>

There are many utilities that can be used to archive/extract, compress/decompress files in Linux, mainly are: tar, gzip, bzip2, zip, etc.
Before we use these utilities, it’s better to be clear about the differences between the meaning of archiving and compressing.

Archiving simply package several files together into a single file, there is no compress procedure involved implicitly, on the other side,
Compressing will package files together and apply some compressing algorithm to shrink the size of the resulting file.

A list of common archived files in Linux

  • .tar
  • .tar.gz
  • .tar.bz2
  • .tar.xz
  • .tar.Z
  • .tgz
  • .zip

Note:
.tar.gz and tar.bz2 are the two most common zipped file format in Linux, .gz implies gzip compressed file, while .bz2 implies bzip compressed (LZ77 algorith is applied) file.

The tar utility is used to create a single archive from several files and extract the files from an archinve.
The gzip, bzip2 or zip utility is used to compress a single file to smaller size.

tar, gzip, bzip2 are provided by default in most Linux/Unix distributions, zip utility may need a manual installation.

Compress Decompress List
gzip filename gzip -d *.gz, gunzip *.gz gzip -l *.gz
bzip2 filename bzip2 -d *.bz2, gunzip data.doc.bz2`
zip data.zip *.doc
zip -r data.zip data/
unzip *.zip unzip -l *.zip
tar -zcvf data.tgz *.doc
tar -zcvf data.tar.gz *.doc
tar -jcvf data.tbz2 *.doc
tar -zxvf data.tgz
tar -zxvf pics.tar.gz *.jpg
tar -jxvf data.bz2
tar -ztf *.tgz
tar -ztf *.bz2
tar -jtf *.bz2

Common options of tar

  • -c: create a new archive
  • -x: extract files from an archive
  • -t: list the contents of an archive
  • -r: append files to the end of an archive
  • -u: update

Note that the above five options are independent and therefore any two of them can not be used together (they can be used combined with other options but not each other).

-c or -x will be used when packaging or extracting.

  • -z, –gzip use gzip compress
  • -j, –bzip2 use bzip2 compress
  • -Z, –compress use for *.tar.Z
  • -v verbose
  • -O, –to-stdout extract files to standard output
  • -C, –directory Output directory

-f option is used to specify the archive name, this is a mandatory option for tar.

Examples

1. Create an archive from a folder

$ tar -cf test.tar.gz mytest/

2. Extract files out of an archive

$ tar -xf test.tar.gz

3. Specify output directory with -C option

$ tar xf test.tar -C other/

Note: use -d option if we use unzip or gunzip