Home Learn Linux How to Untar Files in Linux: A Comprehensive Guide

How to Untar Files in Linux: A Comprehensive Guide

This guide explains the process of extracting files from tar archives, the common use cases of tar archives in Linux, and the difference between "xvf" and "xzf" options in the "tar" command.

by Divya Kiran Kumar
Published: Updated:
untar files in linux

Tar is a popular archiving format used in Linux that allows you to combine multiple files or directories into a single file. This is useful for transferring or storing large amounts of data in a compact format. To access the files within a tar archive, you need to extract or untar them using the command line interface in Linux.

Untarring files in Linux can be accomplished using the “tar” command. The “tar” command creates, manipulates, and extracts tar archives. It is a powerful command-line tool that provides a range of options and parameters for working with tar archives. Before heading to the tutorial, we want to brief you quickly about tar vs. zip and its typical applications in the Linux world.

Tar vs. Zip: Key differences

Linux uses tar as its default archiving format due to its compatibility with Unix-based systems and its ability to preserve file permissions and ownership information during archiving. Tar is also a very efficient format for creating archives of large collections of files and directories.

While tar and zip are archiving formats, they have some key differences. Tar archives do not compress the files they contain by default, whereas zip archives usually include compression. Tar archives also preserve file ownership and permissions, which can be crucial in certain situations, whereas zip archives do not always do so. Additionally, zip is a more widely supported format on non-Unix systems, whereas tar is often the preferred format for Linux and Unix-based systems. So, does it mean there is no compression in Linux tar files? Thankfully, no. Let us see the various tar file extensions in Linux.

Common file extensions for Tar archives

The extension of a tar archive file can indicate how it was compressed, which can help you choose the appropriate tool for extracting the archive.

  • .tar: This is an uncompressed tar archive’s most common file extension.
  • .tar.gz: This file extension is used for a tar archive that has been compressed using gzip.
  • .tar.bz2: This file extension is used for a tar archive that has been compressed using bzip2.
  • .tar.xz: This file extension is used for a tar archive that has been compressed using xz.

Where are tar files used in Linux?

  1. Archiving and compressing files: Tar can combine multiple files or directories into a single tar archive file, which can then be compressed using a tool like gzip or bzip2 to reduce its size.
  2. Backing up data: Tar is a popular tool for creating backups of important data on Linux systems. Creating a tar archive of important files and directories allows you to restore them quickly in case of data loss or system failure.
  3. Distributing software: Many software packages in Linux are distributed as tar archives. This makes it easy to distribute and install software across different Linux systems.
  4. Transferring files: Tar archives can be used to transfer large files or directories over a network or between different systems. They can be easily compressed and decompressed, making them an efficient way to transfer data.
  5. Building packages: Many Linux packages are built from source code distributed as a tar archive. By extracting the source code from the tar archive, you can configure and compile the software to create a package that can be installed on your system.

A step-by-step guide to untar files in Linux

We will show you how to untar a source code tar file for the tutorial. Our downloaded file is perl-5.36.0.tar.gz. As you can notice, the file extension is compressed using gz.

example tar file perl source code

Example tar file – Perl Source Code

You need to access the command line interface to untar files in Linux. First, open a terminal by pressing the “Ctrl + Alt + T” keys on your keyboard or searching for “Terminal” in the Applications menu.

Navigate to the directory: Use the “cd” command to navigate to the directory where the tar archive is located. For example, if the tar archive is located in the Downloads directory, enter the following command:

cd ~/Downloads
using cd command to navigate to the tar file

Using the cd command to navigate to the tar file

View the contents of the tar archive: Use the “tar -tf” command to view the contents. For example, if the name of the tar archive is “archive.tar”, enter the following command:

tar -tf archive.tar

This command will display a list of files and directories in the tar archive. In our case, we use tar -tf perl-5.36.0.tar.gz

view contents of the tar file

View contents of the tar file

Untar the files: Use the “tar -xvf” command to extract the files from the tar archive. If the file is compressed, use “tar -xzf” instead. For example, if the name of the tar archive is “archive.tar”, enter the following command:

tar -xvf archive.tar

This command will extract all the files and directories in the tar archive into the current directory. In our example, we run tar -xzf perl-5.36.0.tar.gz

extract perl source code tar file

Extract Perl Source Code Tar File

Specify a destination directory: If you want to extract the files to a specific directory, use the “-C” option followed by the directory path. For example, if you’re going to extract the files to the “/opt” directory, enter the following command:

tar -xvf archive.tar -C /opt

This command will extract all the files and directories in the tar archive into the “/opt” directory.

Extract a specific file or directory: If you only want to extract a particular one from the tar archive, specify its name at the end of the “tar -xvf” command. For example, if you want to extract only the file “file.txt” from the tar archive, enter the following command:

tar -xvf archive.tar file.txt

This command will extract only the “file.txt” from the tar archive into the current directory.

That’s it! You have successfully untarred files in Linux using the command line interface.

Conclusion

Untarring files in Linux is a simple process that can be accomplished using the “tar” command in the command line interface. By following the steps outlined in this guide, you can easily extract files from tar archives and access their data. Additionally, we have discussed the everyday use cases for tar archives in Linux, including archiving and compressing files, backing up data, distributing software, transferring files, and building packages. Finally, remember that the file extension of a tar archive can give you an indication of how it was compressed, and you can use the appropriate option (such as “xvf” or “xzf”) to extract the archive accordingly. With this knowledge, you can effectively manage and extract tar archives on your Linux system.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.