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?
- 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.
- 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.
- 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.
- 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.
- 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.
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:
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
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
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.
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.