Linux - How to compress a tar file

By xngo on June 12, 2019

Overview

tar is a program that will pack all files into 1 archive file. From here, you then can compress that archive file using different compression algorithm such as gzip or bzip2. This tutorial will show you how to use both of them

Compress using gzip and bzip2

# Compress using gzip.
#  Compress all content of /path/to/dir/, file1 and file2 into archive.tar.gz
tar -czvf archive.tar.gz /path/to/dir/ file1 file2
 
# Compress using bzip2.
#  Compress all content of /path/to/dir/, file1 and file2 into archive.tar.bz2
tar -cjvf archive.tar.bz2 /path/to/dir/ file1 file2
  • c: Create a new tar file.
  • z: Use gzip to compress.
  • j: Use bzip2 to compress.
  • v: Verbose. Show what it is doing.
  • f: Create the tar file with filename provided as the argument

Uncompress

# Uncompress gzip file.
tar -xzvf archive.tar.gz
 
# Uncompress bzip2 file.
tar -xjvf archive.tar.bz2
  • x: Extract the file.

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.