How to compress and extract files using tar command in Linux [Tip]                         

Compressing a file or directory is a very useful task and we are always in need of compressing something. The compression process on different operating systems varies from each other.in windows it is quite simple because majorly we are dealing with a graphical user interface or simply we say a GUI. But on the other hand it is quite different and I must say relatively difficult if we have to do it on a terminal as in Linux or in command prompt. Because we have to deal with commands. And for a person who never used anything other than a GUI it will be hell. But don’t you worry it is much easy as it looks we will guide you through how to compress extract using tar command on Linux.

The tar command on In Linux we often use command named tar to create .tar.gz or .tgz files, which are also known as tarballs. This command contains variety of options at ones disposal which are complicated as whole, but we don’t have to get in those complications just remember few letters to create archives quickly and easily using tar. Using tar command you can also extract archives.tar-icon-13497

Following are few utilities tar command provides.

Compress an Entire Directory or a Single FileScreenshot-Terminal

For compressing an entire directory or a single file we use this command in Linux. It automatically compresses every other directory inside that directory you have specified.

tar -czvf name-of-archive.tar.gz /path/to/directory-or-file

We will decode above command so you can understand what is really going on in there. And what these switches are doing:

  • -c: it means simply create the archive.
  • -z: it says compress archive with the zip format.
  • -v: it for displaying the progress bar in terminal while compression is taking place it is optional in these commands, but we can’t its help.
  • -f: it allows you to choose the filename of archive.

Live Example

Let us consider an example for our ease .consider we have directory named Spike in current directory and we simply want to save it to a file named archive.tar.gz.For achieving this we’d run this command:

tar –czvf archive.tar.gz Spike

Or there is a directory whose path is /usr/local/random on current system and we want to get it compressed to a file archive.tar.gz.We’d run this command:

                        tar –czvf archive.tar.gz /usr/local/random

Extract an Archive

Once we made the archive, next step is extracting it when we are in need of the file. To extract the file using tar command. We will be using following command which would extract contents of archive.tar.gz for us.

                       tar –xzvf archive.tar.gz

You can notice that above command is same as the command we have used in archive creation, just the -x switch comes in place of the -c switch. This will tell the terminal that you want to extract the archive.

There is also a discrete command to extract the contents of a specific directory. You can do that by moving the -c switch to very end of command.

For example, command given below will extract contents of archive.tar.gz to /amp directory.

tar –xzvf archive.tar.gz -c /amp

This is it guys these are ways you can adopt to compress and extract using tar command in Linux. Just carefully follow the steps mentioned above and you will definitely achieve the results that you need!

Related Posts