gz, bz2, xz, and tar

Isshiki🐈
1 min readJun 22, 2020

tar is an archiving program and it doesn’t compress files.

gzip (.gz), bzip2 (.bz2) and xz(.xz) are all data compression tools and they don’t archive files.

To do what people do with WinRAR and 7-Zip, people usually use tar along with one of the data compression tools mentioned above to created compressed archives (.tar.gz, .tar.xz, or .tar.bz2).

To use tar along with these compression tools, you pass options to tar, like

tar abcd file

Despite its staggering man page, I usually use only three or four options. Some of them include:

  1. a is optional and specifies the compression tool, z for gzip, j for bz2 and J for xz
  2. b specify the desired operation, c for create, x for extract and t for inspect (table)
  3. c is optional and toggle the verbose mode, v for on
  4. d is optional and tells tar that you are going to provide a filename, f for on

And here are some examples of tar:

  1. Decompressing and extract all the files in a .tar.gz file: tar zxvf a.tar.gz
  2. Archive and compress multiple files using tar and xz: tar Jcvf ar.tar.xz *.log
  3. The command above is the same as: tar cvf ar.tar *.log; gzip ar.tar
  4. Inspect the content of a compressed archive file: tar Jtvf ar.tar.xz

UPDATE:

A better way to memorize these commands would be

tar xzf a.tar.gz 
# extract a gzip file
tar xJf a.tar.gz
# extract a xz file
# btw, if you want to put the extracted files into a new directory
tar -C directory xzf a.tar.gz

--

--