Learn how to untar, unzip, and extract tar files or tar.gz archives in Linux. In Linux and Unix-based systems, files are often compressed using formats like .tar, .tar.gz, or .tgz. These methods are widely used for packaging software and other downloadable content. Mastering how to create a tarball, view the contents of a tar archive, append to a tar file, and extract or untar files is essential for efficiently managing and using these archives.
What is the Tar Command?
Tar stands for "tape archive" and is GNU software originally developed for backing up data to tape drives. It is still used today for consolidating and compressing data. The tar command is used in Unix and Linux systems to bundle multiple files and directories into a single archive file, simplifying the management, storage, and transfer of large sets of files. Archives created with tar are typically saved with a .tar extension and can also be compressed to save space, resulting in files like .tar.gz or .tar.bz2.
How to Create a Tarball
Creating a tarball from a directory of stored files is relatively simple when performed from the command line. For Linux users comfortable with navigating and using the terminal, here's how to do it:
- Navigate to the Directory: Open your terminal. Use the cd command to change to the directory where your files are located.
- Execute the Tar Command: Use the tar command to create your tarball. Here’s an example:
tar -czvf archive-name.tar.gz .
- Locate the Tarball: After the command completes, you'll find your tarball named archive-name.tar.gz in the same directory.
Using this method lets you quickly package files into a tidy tarball.
What is the Untar Command in Linux?
In Linux, the term "untar command" isn't a standalone command. Instead, it commonly refers to the process of extracting, unzipping, or uncompressing tar and tar.gz files from a tar archive. The actual command used for this purpose is the tar command, combined with specific extraction options or arguments, enabling you to untar files, as we will cover next.
Untar Command Options
Here is a breakdown of what each tar
command option or argument does when used to list, extract, or modify .tar
, .tar.gz
, and .tgz
files:
Untar Linux Option | What the Untar Command Option Does |
---|---|
-x | Extract files from an archive. |
-z | Use gzip compression to extract the archive. |
-v | Be verbose; display the progress and file list. |
-f | Specify the input archive file (e.g., .tgz or .tar.gz). |
-t | List the contents of an archive without extracting. |
-C /myfolder | Change to the specified directory before extracting files. |
-r | Append files to an existing archive. |
How to Preview the Contents of a Tar Archive
Before extracting a .tar or .tar.gz file, it's always a good idea to check what's inside to avoid cluttering your system with unnecessary files. You can do this using the following tar command:
tar -tvf yourfile.tar.gz
Breakdown of Options:
- -t: List the contents of the archive.
- -v: Provide detailed output (optional).
- -f: Specify the archive file.
Note: The -z option is not needed here as it is only required when extracting a compressed .tar.gz file.
Why This Matters:
- Prevents extracting unwanted files.
- Saves time by avoiding unnecessary clutter.
- Keeps your workflow organized and efficient.
With this single command, you can preview the contents of your archive quickly and easily before proceeding to untar.
How to Untar a Tar File
You can untar or unzip tar files in Linux by using the tar command along with the -xzvf options. Here's how:
- Open a terminal; ctrl+alt+t
- From the terminal, change to the directory where your .tar file has been downloaded.
cd ~/directory_path
- To extract or tar unzip the file to the current directory, type the following command,
(Making sure to replacefile_name.tar
with the actual filename)tar -xvf file_name.tar
You can also specify a different directory to extract to using the -C parameter and path to the directory as follows:
tar -C /myfolder -xvf file_name.tar
How to Untar Tar.gz Files
To extract or untar tar.gz files in Linux or Unix from an open terminal:
- Change directory to where your .tar.gz file is located,
cd ~/directory_path
- To extract the contents of the tar.gz file to the current directory,
(replacingfile_name.tar.gz
with the actual name of your file), use the following command:tar -zxvf file_name.tar.gz
Note that this process also works to decompress and extract the contents of a .tgz file:
tar -xzvf file_name.tgz
Or to extract to another directory, type the following, changing /myfolder to the path you want to extract to:
tar -C /myfolder -zxvf file_name.tar.gz
Adding Files to an Existing Tarball
When it comes to updating a tarball, or a .tar.gz file, you don't need to extract its contents first. If you're using a Linux operating system, you can add new files directly to the archive, similar to how you would copy files into a folder.
tar -rvf archive.tar.gz newfile.txt
Note: Replace archive.tar.gz with the name of your existing tarball, and newfile.txt or whatever newfile name and extension with the file you want to add. You can add multiple files at once by listing them separated by spaces.
By following this step, you'll seamlessly be able to integrate new files into your existing tarballs without the hassle of decompression.
Conclusion:
There you have it. A few simple commands are all it takes to untar, unzip or extract tar gz files from within running Linux or Unix operating environments. Hopefully this has helped you decompress, unpack and extract those compressed tar and tar gz files you downloaded from the internet. If you are looking for additional helpful solutions, you might want to check out this right mouse click open files as root article.