In this tutorial we will show you How to use the tar command.
In Linux, you can extract tar files using the terminal application only. So first of all, let’s move to the desktop and list the contents by using the ls command.
Step 1 – Archive and compress the folder
First of all, in the terminal application, type cd Desktop and hit enter.
Here you can see that there is one folder placed in the desktop, named Test1. Now let’s archive the folder and compress it by means of the tar commands.
Step 2 – Understand how to archive folders
When using the tar command, you have to know that it works with certain flags. For example to archive a folder, we will use the “cvf” attribute comprising of 3 flags. The “C” flag means creating a new archive. The “V” flag is used to print, or list the files being processed and the “F” flag uses the defined archive variable. If not specified, the tar command will make a system-dependent file name.
Now let’s type in the tar command:
“tar –cvf Test1.tar ” followed by the name of the folder which in this case is “Test1” and hit enter.
Now when we list the directories again, you can see the tar file that we have just created, appears in Red color in the terminal.
Step 3 – Extract a tar archive
In the same way, we can extract a tar archive as well. For that, you can simply type:
“tar -xvf” followed by the name of the file which in this case is “Test1.tar”. The “X” flag over here is used to extract the contents of the archive.
Now when we list the contents of the directory, you can see that the tar file has been successfully extracted.
And that’s it, by using the tar commands you can archive folders and extract them.