Linux: Compression How-to: tar gzip bzip2 xz 7zip rar zip

By Xah Lee. Date: . Last updated: .

This page shows you how to decompress or compress file or directory in Linux.

tar cvf newName.tar dirPath
Tar up a dir.
tar xvf fileName.tar
Untar.
zip -r newName.zip dirName
Zip a dir
unzip fileName
Unzip
gzip fileName
Compress a file.
gzip -d fileName
Decompress a file.
bzip2 fileName
Compress a file.
bzip2 -d fileName
Decompress a file.
xz fileName
Compress a file.
xz -d fileName
Decompress a file.

tar archive (.tar)

“tar” is for making a directory into a single file. Tar does not compress. Tar is often used with a compression utility such as gzip.

archive a folder
tar cvf newName.tar dirName

Here's most frequently used options:

  • v → verbose, meaning print out status.
  • f → file.
  • c → create.
  • x → extract.
  • t → test/list content.

“tar” can untar and decompress in one step:

Extract .tar
tar xvf fileName
Extract .tar.gz
tar xvfz fileName
Extract .bz2
tar xvfj fileName
Extract .xz
tar xvfJ fileName
List content of .tar
tar -tvf fileName

gzip compression (.gz)

gzip is unix standard for compressing a single file.

Compress
gzip fileName
Decompress
gzip -d fileName or gunzip fileName
Check integrity
gzip -t fileName

bzip2 compression (.bz2)

bzip2 is for compressing a single file.

Compress
bzip2 fileName
Decompress
bzip2 -d fileName

xz compression (.xz)

xz is for compressing a single file.

Compress
xz fileName
Decompress
unxz fileName or xz -d fileName

zip archive (.zip)

ZIP (file format) is for archiving and compressing a folder or single file.

Create archive
zip -r newName.zip dirName
Extract
unzip fileName
Check integrity
zip -T name.zip

7-zip archive (.7z)

7-zip is for archiving and compressing a folder.

The 7z util can decompress many formats.

Create archive
7z a -t7z newName.7z dirName
Extract
7z e fileName

rar archive (.rar .rev .r00 .r01 .r02 …)

RAR (file format) is for archiving and compressing a folder. File extension: {.rar .rev .r00 .r01 .r02 …}.

Extract rar file
unrar e fileName
Extract rar file
7z e fileName

Compression File Size Comparison

Linux Shell Basics

Sys Admin

Bash/Terminal

Linux Desktop