Saturday, April 20, 2024
HomeA-Z 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: 

–cIt creates an archive file.
–fIt creates an archive alongside the provided name of the file.
–uIt adds an archive to another existing archive file.
–vIt displays verbose information.
–jIt filters archive tar files using the help of tbzip.
–zIt provides the details about those tar files which are created with gzip.
–WIt verifies an archive file.
–tIt lists or displays files inside the archived file.
–xIt extracts the archive file.
–AIt concatenates the archive file.

Useful Examples of Tar Command in Linux

In this section, we will explain different examples that are very useful for knowing more about Tar. 

1. Create a Tar Archive File

Suppose we want to create a 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:

tar xvf Ubuntupit.tar

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

3. Create a tar.gz File

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

tar cvzf Ubuntupitfile.tar.gz *.deb

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:

tar -xvf Ubuntupitfile.tar.gz

5. Create a tar.bz2 File

The bz2 option can compress and create files with smaller sizes than 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:

tar cvfj MyUbuntupit.tar.bz2 *.deb

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:

tar -xvf MyUbuntupit.tar.bz2

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 the pyload_0.4.9_all.deb file from Ubuntupit.tar, so we will execute the following commands:

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

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:

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

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: 

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

You can 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

The tar command in Linux lets you 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.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

You May Like It!

Trending Now