Compress and Uncompress Files with tar Command in Linux

In this tutorial, you are going to learn how to compress and uncompress files with tar command in Linux. The tar is an acronym for tape archiver. It is a command line utility used to create tar archives by converting a group of files into an archive. Selected files are copied and stored in a single file known as tar archive file. If this archive file is further compressed using a data compression utility, the compressed archive file is called a tarball.

Compress and Uncompress Files with tar Command in Linux

Command line syntax of tar:

tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME(S)]
  • OPERATION – Only one operation argument is allowed and required at time:

-c Create a new tar archive.

-t Display a list of the files included in the archive.

-x Extract the entire archive or one or more files from an archive.

  • OPTIONS -The most options used are:

-v Show the files being processed by the tar command.

-f Specifies the archive file name.

  • ARCHIVE_NAME -The name of the archive.
  • FILE_NAME(S) -A space-separated list of file names to be archived, compressed or extracted.

In general to archive a single directory or a single file on Linux, we use:

tar -cvf NAME-OF-ARCHIVE.tar /PATH/TO/DIRECTORY-OR-FILE

Archive/Compress Files Using tar Command

Creating an Archive

With tar command you can create an archive of file in a directory. i.e ~/Downloads. An archive Mybooks.tar will be created in the current working directory ~/Archives.

tar -cvf Mybooks.tar ~/Downloads
/home/thehero/Downloads/
/home/thehero/Downloads/25 Days of Christmas Writeup (Blurred).pdf
/home/thehero/Downloads/vagrant_2.2.14_x86_64.deb
/home/thehero/Downloads/HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf
/home/thehero/Downloads/xampp-linux-x64-7.4.11-0-installer.run
/home/thehero/Downloads/LPIC-1 Linux Professional Institute Certification Study Guide [BooksRack.net].pdf

Now, if we list the contents of the current working directory ~/Archives we will find Mybooks.tar archive.

ls -lh ~/Archives
-rw-rw-r--  1 thehero thehero 211M Mar 25 08:18 Mybooks.tar

Creating a Tarball

It’s a good idea to employ compression utility especially when when backing up large amount of data and with tar command you will be able to accomplish that task. You can use different options to obtain different compression methods:

1. Using gzip

Option -z compresses tar archive file into tarball using gzip.

tar -zcvf Mybooks.tar.gz ~/Downloads
/home/thehero/Downloads/
/home/thehero/Downloads/25 Days of Christmas Writeup (Blurred).pdf
/home/thehero/Downloads/vagrant_2.2.14_x86_64.deb
/home/thehero/Downloads/HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf
/home/thehero/Downloads/xampp-linux-x64-7.4.11-0-installer.run
/home/thehero/Downloads/LPIC-1 Linux Professional Institute Certification Study Guide [BooksRack.net].pdf

List the contents of ~/Archives directory.

ls -lh ~/Archives 
-rw-rw-r--  1 thehero thehero 211M Mar 25 08:18 Mybooks.tar
-rw-rw-r--  1 thehero thehero 206M Mar 25 10:21 Mybooks.tar.gz

There are different sizes of files in the directory i.e. 211M and 206M showing that Mybooks.tar has been compressed to Mybooks.tar.gz tarball.

2. Using bzip2

Option -j Compresses tar archive file into a tarball using bzip2.

tar -jcvf Mybooks.tar.bz2 ~/Downloads
/home/thehero/Downloads/
/home/thehero/Downloads/25 Days of Christmas Writeup (Blurred).pdf
/home/thehero/Downloads/vagrant_2.2.14_x86_64.deb
/home/thehero/Downloads/HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf
/home/thehero/Downloads/xampp-linux-x64-7.4.11-0-installer.run
/home/thehero/Downloads/LPIC-1 Linux Professional Institute Certification Study Guide [BooksRack.net].pdf

List the contents of ~/Archives directory.

ls -lh ~/Archives 
-rw-rw-r--  1 thehero thehero 211M Mar 25 19:58 Mybooks.tar
-rw-rw-r--  1 thehero thehero 207M Mar 25 20:05 Mybooks.tar.bz2

There are different sizes of files in the directory i.e. 211M and 207M showing that Mybooks.tar has been compressed to Mybooks.tar.bz2 tarball.

3. Using xz

Option -J Compresses tar archive file into a tarball using xz.

tar -Jcvf Mybooks.tar.xz ~/Downloads
/home/thehero/Downloads/
/home/thehero/Downloads/25 Days of Christmas Writeup (Blurred).pdf
/home/thehero/Downloads/vagrant_2.2.14_x86_64.deb
/home/thehero/Downloads/HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf
/home/thehero/Downloads/xampp-linux-x64-7.4.11-0-installer.run
/home/thehero/Downloads/LPIC-1 Linux Professional Institute Certification Study Guide [BooksRack.net].pdf

List the contents of ~/Archives directory.

ls -lh ~/Archives 
-rw-rw-r--  1 thehero thehero 211M Mar 25 19:58 Mybooks.tar
-rw-rw-r--  1 thehero thehero 205M Mar 25 20:12 Mybooks.tar.xz

There are different sizes of files in the directory i.e. 211M and 205M showing that Mybooks.tar has been compressed to Mybooks.tar.xz tarball.

Listing Tarball Contents

Use option -t to list the contents of the tarball or archive files.

tar -tf Mybooks.tar.gz
home/thehero/Downloads/
home/thehero/Downloads/25 Days of Christmas Writeup (Blurred).pdf
home/thehero/Downloads/vagrant_2.2.14_x86_64.deb
home/thehero/Downloads/HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf
home/thehero/Downloads/xampp-linux-x64-7.4.11-0-installer.run
home/thehero/Downloads/Mybooks.tar.gz
home/thehero/Downloads/LPIC-1 Linux Professional Institute Certification Study Guide [BooksRack.net].pdf

Unpack/Extract/Uncompress Files Using tar Command

Extracting an Archive

The command is the same as the archive creation command used above, except the -x switch is replaced with the -c switch.

To extract the contents of the archive to a specific directory we use -C.

tar -xvf Mybooks.tar -C ~/Extracted
home/thehero/Downloads/
home/thehero/Downloads/25 Days of Christmas Writeup (Blurred).pdf
home/thehero/Downloads/vagrant_2.2.14_x86_64.deb
home/thehero/Downloads/HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf
home/thehero/Downloads/xampp-linux-x64-7.4.11-0-installer.run
home/thehero/Downloads/LPIC-1 Linux Professional Institute Certification Study Guide [BooksRack.net].pdf

We can list the contents of ~/Extracted directory.

ls -lh ~/Extracted/home/thehero/Downloads
-rw-rw-r-- 1 thehero thehero 9.7M Sep 19  2020 '25 Days of Christmas Writeup (Blurred).pdf'
-rw-rw-r-- 1 thehero thehero 588K Sep 25 19:07 'HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf'
-rw-rw-r-- 1 thehero thehero  14M Mar 23 13:27 'LPIC-1 Linux Professional Institute Certification Study Guide [BooksRack.net].pdf'
-rw-rw-r-- 1 thehero thehero  38M Mar  4 21:36  vagrant_2.2.14_x86_64.deb
-rwxr-xr-x 1 thehero thehero 150M Oct 26 11:01  xampp-linux-x64-7.4.11-0-installer.run

Extracting a Tarball

Same case here, the command is the same as the tarball creation command used above, except the -x switch is replaced with the -c switch.

To extract the contents of the archive to a specific directory we use -C.

You can use different options to obtain different decompression methods:

1. Using gunzip

Option -z decompresses files in a tarball using gunzip.

tar -xzvf Mybooks.tar.gz -C ~/Extracted
home/thehero/Downloads/
home/thehero/Downloads/25 Days of Christmas Writeup (Blurred).pdf
home/thehero/Downloads/vagrant_2.2.14_x86_64.deb
home/thehero/Downloads/HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf
home/thehero/Downloads/xampp-linux-x64-7.4.11-0-installer.run
home/thehero/Downloads/LPIC-1 Linux Professional Institute Certification Study Guide [BooksRack.net].pdf
2. Using bunzip2

Option -j Decompresses files in a tarball using bunzip2.

tar -xjvf Mybooks.tar.bz2 -C ~/Extracted
home/thehero/Downloads/
home/thehero/Downloads/25 Days of Christmas Writeup (Blurred).pdf
home/thehero/Downloads/vagrant_2.2.14_x86_64.deb
home/thehero/Downloads/HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf
home/thehero/Downloads/xampp-linux-x64-7.4.11-0-installer.run
home/thehero/Downloads/LPIC-1 Linux Professional Institute Certification Study Guide [BooksRack.net].pdf
3. Using unxz

Option -J Decompresses files in a tarball using unxz.

tar -xJvf Mybooks.tar.xz -C ~/Extracted
home/thehero/Downloads/
home/thehero/Downloads/25 Days of Christmas Writeup (Blurred).pdf
home/thehero/Downloads/vagrant_2.2.14_x86_64.deb
home/thehero/Downloads/HOW TO CREATE USERS AND COMPUTER VIA GROUP POLICY.pdf
home/thehero/Downloads/xampp-linux-x64-7.4.11-0-installer.run
home/thehero/Downloads/LPIC-1 Linux Professional Institute Certification Study Guide [BooksRack.net].pdf

That’s all on how to compress and uncompress files with tar command in Linux.

Also check;

How to use netstat command in Linux

Leave a Comment