How to Compress and Extract Files Using the Tar, Zip Command on Linux

How to Compress and Extract Files Using the Tar

Zip and tar are popular command-line utility for compressing and archiving multiple directories and files into one archive file in a Linux system. By default, the tar command doesn’t compress files but only collects files and their metadata to generate a single tar file but we can archive files using gzip/bzip2. Whereas, zip provides lossless data compression which is a data compression algorithm class that allows compressed data is reconstructed to its original form. The gzip and zip uses DEFLATE algorithm for compressing files whereas bzip2 uses the Burrows-Wheeler algorithm that produces highly compressed file but consumes more time as compare to gzip and zip.


By default, the tar command is integrated with the Linux system so let’s begin with installing zip and unzip. The unzip command won’t be installed while installing zip command so we need to install it separately.

Before continue with installation don’t forget to update the package info.

$ sudo apt-get update

Installing Zip.

$ sudo apt-get install zip

Installing Unzip

$ sudo apt-get install unzip

For showing the following example, I have used Ubuntu 20.4 and has the following files in the home directory.

Some Examples of Tar Commands

Before continuing with the example you must know the syntax of the tar so the syntax is like this:

$ tar -c [option] [archive name] [file] | [directory] |

The following options are the main and compulsory options that lead the command to what task should be performed. The following options can be used once in the command without it the command won’t be run.

-c: To generate archive file.

-r: To add more files to the existing archive file.

-t: View all files inside the archive.

-x: To extract the archive files.

Compressing Files Using gzip and bzip2

Tar command produces a compressed file using gzip and bzip2 which can be accessed by providing the -z and -j options to the command respectively. For archiving and compressing tar files we need to execute the following command.

$ tar -cvzf compress_sample.tar.gz *.txt

Compressing file using gzip.

$ tar -cvjf compress_sample.tar.bz2 *.txt

Compressing file using bz2.

In the above example, the -c option refers to create the archive, the -v option displays the verbose of the archived files, and -f denotes the name of the archive.

Note: -f option always must be before the filename.

Extracting Files From Compressed File

For extracting the compressed file you need to run:

$ tar -xvf compress_sample.tar.gz

The above command will extract file in the current working directory but if want to extract files in a different directory you can do so by using -C to the command.

$ tar -xvf compress_sample.tar.gz -C Documents/

Extracting files to different dir using tar.

Extracting Specific Files From Compressed File

We can extract specific files from the compressed file for that we need to specify the file name along with the directory name if it is inside the directory.

$ tar -xvf compress_sample.tar sample.txt student.txt

Extracting specific files using tar.

Some Examples of Zip and Unzip Command.

Syntax for zip and unzip are as follows:

$ zip [option] [archive_name] [Files]
$ unzip [option] [archive_name]

Compressing Files Using Zip

Zip is the archive compressor so we don’t need to provide any extra parameter to compress the files. For compression we need to run the command like this:

$ zip zip_demo.zip *.txt

Zip multiple files

Unzipping File to Different Directory

We can unzip the zip files to the different directories using the -d option and specifying the directory in the command.

$ unzip zip_demo.zip -d Documents/

Unzip the file to a different directory.

Unzipping Specific Files From Compress File

To unzip a specific file from the archive the command must be executed in the following way.

$ unzip zip_demo.zip student.txt sample.txt

Unzip specific files from the zip file.


In this article, we learn how to archive and compress files using tar and zip commands. I have included the most common example in the article hope this will help you how to compress files.

Similar Posts