tar command in Linux and zip command With Examples

Hello everyone In this Article we are going to learn how we can Compress & Uncompress Files & Directories in Linux in a very simple and easy step-by-step approach.

tar command in linux

tar command in Linux

Suppose we have one server and daily 10GB of data is writing on that server and we need to take the backup of that data on daily basics.

Instead of backup of 10GB of data, we can compress that 10GB of data to 2GB of data then we can take the backup this helps to save the cost of storage.

When we want to retrieve the data we can uncompress that data and retrieve it.

Archiving of Files

Suppose we have 5 files ex. File1.txt, File2.txt, File3.txt archiving means that we can collect these files in one place, and after collecting the files we can compress that tar file. And originally the size of data is 30MB. After archiving the data it’s size will be reduced to 26MB

We can further compress the archive in order to reduce the size more. For compressing we can use 3 methods

  • Gunzip – If we choose the Gunzip method the size of archived data will be approx 8MB. (. gz ) is extension
  •  Bunzip – If we choose the Bunzip method the size of archived data will be approx 7MB. ( .bz2 ) is extension
  •  xz– If we choose xz method then the size of archived data will be approx 5MB. ( .xz ) is the extension

The tar command is use to make the archive for the files/directories.

Before Jumping on the command we should know the syntax of the tar command

tar [options]… Archive-folder-name files&folders

1 – How to check the size of files & directory

To check the size of the directory then we can use the below command. Here we are checking the size of /etc/ directory.

# du -sh /etc/
Screenshot 1 39

2 – How to archive the directory

The tar command is use to archive the directory.

In this command -c ( create the archive file )

-v (verbose the content )

-f (file)

And then we can write any name with .tar extension in last we can define the directory that we have to archive.

 # tar -cvf example.tar /etc/
Screenshot 3 38

Now when we check the size of archive directory it is still same as the original one .

Screenshot 4 37

3 – How to extract the archive

To extract the archive that we create we can use ( -x ) option in tar command

# tar -xvf example.tar
Screenshot 5 37

4 – How to view the content of archive file

To view the archive file content that we create we can use the ( -t ) option in the tar command.

# tar -tvf example.tar
Screenshot 6 37

5 – How to extract the archive in different locations

To extract the archive file in a different location we create we can use the ( -C ) option with the location where we want to extract in the tar command. Here we are extracting in /tmp/ directory

#  tar -xvf example.tar -C /tmp
Screenshot 7 29

How to compress the Archive File that we created

There are 3 methods with which we can compress the Archive File

How to compress archive file using Ginzip method

 If we want to compress using the gunzip method then we can execute this command where ( -z ) is the option that is used to compress the tar file using the gunzip method,

# tar -zcvf example.tar.gz /etc/
Screenshot 8 28

Now when we check the size of compress file it is reduced more.

Screenshot 9 29

How to extract the Compressed file that we created above. Just add ( -x ) option with the compressed file

#  tar -zxvf example.tar.gz
Screenshot 10 27

How to compress archive file using bz2 method

 If we want to archive and compress using the bz2 method then we can execute this command where  ( -j ) is the option that is used to compress the tar file using the bz2 method,

# tar -jcvf example.tar.bz2 /etc/
Screenshot 11 28

Now when we check the size of compress file it is reduced more.

Screenshot 13 23

How to extract the Compressed file that we created above. Just add ( -x ) option with the compressed file

# tar –jxvf example.tar.bz2
Screenshot 15 13

How to compress archive file using xz method

To archive and compress using the xz method then we can execute this command where ( -J ) is the option that is used to compress the tar file using the xz method,

 # tar -Jcvf example.tar.xz /etc/
Screenshot 16 11

Now when we check the size of compress file it is reduced more.

Screenshot 17 12

How to extract the Compressed file that we created above. Just add ( -x ) option with the compressed file

# tar -Jxvf redhat.tar.xz
Screenshot 2 32

How Zip and Unzip the file and folders –

These days we are using zip method to compress the files and folder because these method are most efficient to compress and uncompress any files and folders

Before using the zip and unzip command please ensure that zip package must be installed in your system to check that the zip package is installed run the # rpm -qa zip command.

If the package of zip is not installed in the system then please run the following command to install the zip package

# yum install zip* -y

If we want to compress the directory using zip method then we can simply use zip command with the directory name

# zip example.zip /etc/
Screenshot 19 10

Now when we check the size of compress file it is reduced .

Screenshot 20 8

If we want to unzip the directory that we compress with zip command then we using zip method then we can simply use unzip command with the directory name.

# unzip example.zip
Screenshot 21 6

How to check version Of the tar command – If we want to check what is the version of the tar command then we can execute the below command

# tar --version
Screenshot 22 6

How to View the Most useful Options in the tar command 

To know the most useful options with the tar command then we can add – -help with the tar command.

Screenshot 23 4

How to check version Of the zip command – If we want to check what is the version of the zip command then we can execute the below command.

# zip --version
Screenshot 3 39

How to View the Most useful Options in the zip command –  If we want to know the most useful options with the zip command then we can add – -help with the unzip command.

# zip --help
Screenshot 4 38

How to check version Of the unzip command – If we want to check what is the version of the unzip command then we can execute the below command.

# unzip --version
Screenshot 5 38

How to View the Most useful Options in the unzip command –  If we want to know the most useful options with the unzip command then we can add – -help with the unzip command.

# unzip --help
Screenshot 6 38

Conclusion

If you want to learn Linux in the most simple language then please click on this link.

If you want to see the man page of tar command online then click on this link

If you want to see the man page of zip command online then click on this link

If you want to see the man page of the unzip command online then click on this link

This is the complete knowledge of how we can compress and decompress files & Directories in the Linux kernel if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on DevOps Articles.

Leave a Comment