Tar Command in Linux: Understand How to Create and Extract

Tar Command in Linux: Understand How to Create and Extract

4 mins readComment
Updated on Sep 5, 2024 16:47 IST

'tar' is a command in Linux used for creating, viewing, and extracting files from archives. It bundles files into a single archive file, compresses them (optional), and preserves directory structures commonly used for backup or distribution purposes. 

Tar Command in Linux

Table of Content

Tar Command

Most Unix software available on the internet is typically disseminated as a tarball. This implies that all the files have been bundled into a tar archive and compressed using gzip to conserve storage capacity. Consequently, the resulting file bears the extension .tar.gz, with .tgz occasionally serving as a shortened alternative.

This article will explore the use of the tar command and various flags associated with this Linux command.

Explore: Online Linux Courses and Certifications

The tar command in Linux has a syntax as shown below:


 
tar [options] [archive-file] [file or directory to be archived]
Copy code

Options:

  • -c : It is used to create archives
  • -x : It is used to extract the archives
  • -f : It is used to create an archive with a specific name
  • -t : It is used to list files within the archived file
  • -u : It is used to add files to an existing archive file
  • -v : It is used to display verbose information
  • -A : It is used to concatenate the archive files
  • -z : It is used to compress the tar file using Gzip
  • -j : It is used to compress the tar file using bzip2
  • -W : It is used to  verify an archive file
  • -r : It is used to update file or directories in already existing .tar file

Now that we know the syntax and various options associated with the tar command, let’s explore some example use cases.

Recommended online courses

Best-suited Other Programming Languages courses for you

Learn Other Programming Languages with these high-rated online courses

Free
10 hours
– / –
– / –
70.5 K
8 months
4.58 K
5 weeks
– / –
60 hours
29 K
8 months
– / –
31 hours

Creating an Archive File

The tar command can generate archive files employing diverse compression algorithms like xz, gzip, and bzip2. Therefore, we generally append the appropriate compression suffix to the compressed file as per the accepted convention.

Using gzip Compression Algorithm

Here we will use the gzip compression algorithm to generate an archive file, namely my_archive, where we will be archiving 3 files, namely test1.txt, test2.txt and test3.txt, using the below tar command:


 
tar -czvf my_archive.tar.gz test1.txt test2.txt test3.txt
Copy code

This command would return the file names that are archived, as shown below:

This will create the my_archive.tar file as shown below:

Using bzip2 Compression Algorithm

Here we will use the bzip2 compression algorithm to generate an archive file, namely my_archive2, where we will be archiving 3 files, namely test1.txt, test2.txt and test3.txt, using the below tar command:


 
Creating an Archive File
Copy code

This command would return the file names that are archived, as shown below:

 

This will create the my_archive2.tar file as shown below:

Extracting an Archive File

There are various ways of extracting an archive file using the tar command. Let’s take a look at a couple of them.

Extract to Current Directory

To extract the files from a .tar to the current directory we can make use of the following command:


 
tar -xvf < file_name > . < compression_algo_extension >
Copy code

For instance, if we are to extract the files from the my_archive2.tar file, we will use the below command:


 
tar -xvf my_archive2.tar.bz2
Copy code

This command would return the file names that are extracted from the archive, as shown below:

Before:

After:

 

Note: Since we used the bzip2 compression algorithm to create the my_archieve2.tar file earlier in the article, we have used the .bz2 extension at the end of our command.

Extract in Seperate Directory

To extract the files from an archive to a separate directory we make use of the -C option with the tar command as shown below:


 
tar -xvf < file_name > . < compression_algo_extension > -C < PATH_TO_DIRECTORY >
Copy code

Take a look at the below example where we extract the files from the my_archive2.tar to a director with same name and path using the below command:


 
tar -xvf my_archive2.tar.bz2 -C my_files
Copy code

This command would return the file names that are extracted from the archive, as shown below:

As we can see in the below image, we have the three files extracted to my_files directory:

Extract Specific Files

To extract specific files from an archive using the tar command, we make use of the below syntax:


 
tar -xvf < file_name > . < compression_algo_extension > < file_names >
Copy code

Take a look at the below example where we extract the file name test1.txt  from the my_archive2.tar to the current directory using the below command:


 
tar -xvf my_archive2.tar.bz2 test1.txt
Copy code

This command would return the file names that are extracted from the archive, as shown below:

Here, we can also see that a new file named test1.txt is in our directory.

List Contents of an Archive

To list the contents of an archive, we make use of the following syntax:


 
tar -jtvf < file_name > . < compression_algo_extension >
Copy code

Take a look at the below example, where we have listed the contents of the my_archive2.tar file using the below command:


 
tar -jtvf my_archive2.tar.bz2
Copy code

The output will be as follows:

Delete File from an Archive

Deleting a file to an existing archive file is not possible due to restrictions imposed in the tar file design to protect its integrity.

For instance, let’s try to delete a file named test1.txt from our existing archive called my_archive2.tar using the below command:


 
tar --delete -f my_archive2.tar.bz2 test1.txt
Copy code

It will throw an error as shown below:

Conclusion!

The tar command is an incredibly flexible and robust tool designed for file management in Linux. It offers many capabilities, allowing you to effortlessly create, extract, and manipulate archives in various formats. With its extensive range of options, the tar command can cater to diverse requirements. By familiarizing yourself with its syntax and options, you can seamlessly incorporate the creation, management, and extraction of archives into your workflows. Acquiring proficiency in the tar command empowers you to handle your file archives with enhanced efficiency and effectiveness.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio