21.9 C
New York
Thursday, June 30, 2022
HomeLinuxLinux Commands14 Basic Tar Commands in Linux with Examples for Newbies

14 Basic Tar Commands in Linux with Examples for Newbies

In Linux, Tar stands for tape archive. One of the important commands for facilitating archive functionality is this command, which creates and extracts archives. We can use the Tar command to create uncompressed and compressed files and modify as well as maintain them. Many people think that Tar is a part of Linux (Linux is Kernel), but in reality, it is a part of the GNU project. So let’s take a look at the most useful examples of Tar Commands in Linux. 

Tar Commands in Linux


Tar command is one of the best options when it comes to extracting files in Linux easily. This command consists of different options, which are as follows: 

Commands Description 
–c It creates an archive file.
–f It creates an archive alongside the provided name of the file.
–u It adds an archive to another existing archive file.
–v It displays verbose information.
–j It filters archive tar files using the help of tbzip.
–z It provides the details about those tar files which are created with gzip.
–W It verifies an archive file.
–t It lists or displays files inside the archived file.
–x It extracts the archive file.
–A It concatenates the archive file.

Useful Examples of Tar Command in Linux


In this section, we will explain different examples which are very useful to know more about Tar. 

1. Create a Tar Archive File


Suppose we want to create tar archive “Ubuntupit.tar” in the Documents directory. That’s why we will execute the following commands in the terminal: 

cd ~/Documents

The above command will select Documents as the current directory for the terminal. 

tar cvf Ubuntupit.tar *.deb

Using this command, we can include all of the .deb files in Ubuntupit.tar, located in the Documents directory.

2. Extract a Tar File


We can xvf command to extract the Ubuntupit.tar file from the terminal:

ls
tar xvf Ubuntupit.tar
ls

(We used ls command two times to show you the changes before and after executing the xvf command.)

3. Create a tar.gz File


For creating the tar.gz file, we need to use the z option in the cvf command. Here we are creating Ubuntupitfile.tar.gz file in the Documents directory through the following commands: 

ls
tar cvzf Ubuntupitfile.tar.gz *.deb
ls

4. Extract a tar.gz File


We can use the xvf option to extract the tar.gz file, and here are the sample commands for it:

ls
tar -xvf Ubuntupitfile.tar.gz
ls

5. Create a tar.bz2 File


Bz2 option can compress and create the files with lesser size as compared to gzip, and it requires the j option in the command. So we can use the below commands to create a MyUbuntupit.tar.bz2 file in the Documents directory:

ls
tar cvfj MyUbuntupit.tar.bz2 *.deb
ls

6. Extract tar.bz2 File


To extract the tar.bz2 file, we can use the xvf option. So you can use the following commands to extract it:

ls
tar -xvf MyUbuntupit.tar.bz2
ls

7. List Content of  a Tar File


If you want to see the available content in the tar file, then you can use the t option. In this example, we will list the content of Ubuntupit.tar using the following commands: 

tar -tvf Ubuntupit.tar

You can also use the same command to list the contents of tar.gz and tar.bz2 files. 

8. Untar a Single File from a Tar File


In this case, we want to extract pyload_0.4.9_all.deb file from Ubuntupit.tar, so we will execute the following commands:

ls
tar -xvf Ubuntupit.tar pyload_0.4.9_all.deb pyload_0.4.9_all.deb
ls

You can also use the below command to extract a single file:

tar --extract --file=Ubuntupit.tar pyload_0.4.9_all.deb

9. Untar a Single file from a tar.gz File


Use the tar command to extract a single file from the tar.gz file:

ls
tar -zxvf Ubuntupitfile.tar.gz pyload_0.4.9_all.deb pyload_0.4.9_all.deb
ls

You can use the below command as well: 

tar --extract --file=Ubuntupitfile.tar.gz pyload_0.4.9_all.deb

10. Untar a Single file from a tar.bz2 File


For extracting a single file from the bz2 file, please use the following commands: 

ls
tar -jxvf MyUbuntupit.tar.bz2 pyload_0.4.9_all.deb
ls

You also use the below command for extracting a single file: 

tar --extract --file=MyUbuntupit.tar.bz2 pyload_0.4.9_all.deb

11. Extract a Specific Group of Files from Tar


Tar command in Linux lets extract a group of files from a .tar file using the wildcards extracting command. Here we are extracting all .deb files from the Ubuntupit.tar file:

tar -xvf Ubuntupit.tar --wildcards '*.deb'

12. Get the Total Size of a Tar File


If you want to check the size of a tar file, then please use the below command, and you can use it for bz2 and gz files as well: 

Tar file:

tar -czf - Ubuntupit.tar | wc -c

Tar.bz2 file:

tar -czf - MyUbuntupit.tar.bz2 | wc -c

Tar.gz file:

tar -czf - Ubuntupitfile.tar.gz | wc -c

13. Verify a Tar File


For verifying the compressed file, we can use the w option in the command like this :

tar tvfw Ubuntupit.tar

14. Add a Single File to the Tar File


You need to add the r option in the command to add a single directory file to the tar file. Here we are adding the Pyload.txt file to the Ubuntupit.tar file, so we have executed the following commands:

tar -rvf Ubuntupit.tar Pyload.txt

You also can use the same command for .bz2 and .gz files like this: 

tar -rvf  MyUbuntupit.tar.bz2 Pyload.txt
tar -rvf  Ubuntupitfile.tar.gz Pyload.txt

Finally, Insight


Tar command in Linux is most commonly used to create and extract an archive. To extract an archive, use the tar –xf command accompanied by the archive name, and if you want to create a new one, use tar –czf accompanied by the archive name, files, and directories that you want to add to the archive.

Sabiha Sultana
Sabiha Sultana
She is a graduate of English Language and Literature from a reputed University. She writes reviews on trending games and apps. In her free time, she does painting and reads books.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Post

Must Read