How to Compress and Extract Files Using the Tar, Zip Command on Ubuntu 20.4 Linux OS ?

An archive file is basically a collection of files and directory stored in one file. Please note that by default the archive file is not compressed. A compressed file uses less disk space.

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. 

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux Commands queries.



How to install zip and unzip on Ubuntu OS ?

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

To install Zip, execute the command:

$ sudo apt-get install zip

To install Unzip, run the command:

$ sudo apt-get install unzip


A few 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.


How to Compress 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. 

To archive and compress tar files we need to execute the following command:

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

To Compress files using gzip, run the command:

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

Here, 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.


How to Extract Files From Compressed File ?

To Extract the compressed file you need to run the command:

$ tar -xvf compress_sample.tar.gz

This 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/


How to Extract 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

This command will Extract specific files using tar.


A few Examples of Zip and Unzip Command

Syntax for zip and unzip respectively are as follows:

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


How to Compress 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

This command will Zip multiple files.


How to Unzip 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/

This will Unzip the file to a different directory.


How to Unzip 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

This will Unzip specific files from the zip file.


[Need urgent assistance in fixing Ubuntu Linux system errors? We can help you. ]

This article covers how to archive and compress files using tar and zip commands with a few examples to show you how it works.

The tar command on Linux is often used to create .tar.gz or .tgz archive files, also called "tarballs".


How to Remove Files from a Tar Archive ?

Use the --delete operation to remove files from an archive.

The following example shows how to remove the file file1 from archive.tar:

$ tar --delete -f archive.tar file1

Related Posts