Tar Command in Linux: Understand How to Create and Extract

Tar Command in Linux: Understand How to Create and Extract

5 mins readComment
clickHere
Updated on Nov 30, 2023 12:22 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

Tar Command in Linux

 

Table of Content

Tar Command

The majority of 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.

In this article, we will explore the use of 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 creates archives
  • -x : It is used to extracts the archives
  • -f : It is used to create archive with specific name
  • -t : It is used to lists 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 compresses the tar file using gzip
  • -j : It is used to compresses the tar file using bzip2
  • -W : It is used to  verify an archive file
  • -r : It is used to update file or directory 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.

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 couple of them.

Extract to Current Directory

To extract the files from a .tar to the current directory wqe 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 be using 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 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

To delete a file to an existing archive file is not possible, due to restrictions imposed in the tar file design to protect it’s 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 a multitude of 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.

Contributed By: Raju Kumar

 

About the Author

This account contains a repository of informative articles by external authors with domain expertise in various aspects of guiding students on how to go about pursuing their undergraduate and postgraduate studies in... Read Full Bio

Comments