How to extract a file or folder from a TAR or TAR.GZ file

A tarball (TAR or TAR.GZ) is a collection of files grouped together as a single archive that makes it easy to store and transfer large numbers of files locally or over the internet. Whenever you need files, you just need to unzip the archive.

Consider a situation where you only need a single file in a large archive. In this case, instead of decompressing the entire archive, you can simply extract the file you need. Let's find out in detail how to do it through the following article!

View the content of Tarball

If you just want to see the contents of the TAR or TAR.GZ file, you don't need to extract the file. Instead, use the following command to see what's inside:

tar -tvf [archive.tar] tar -ztvf [archive.tar.gz]

This will print a list of all the files and folders inside the archive.

Picture 1 of How to extract a file or folder from a TAR or TAR.GZ file

Extract a file from Tarball

To extract a file from TAR or TAR.GZ, use the following command format:

tar -xvf [archive.tar] [path to file] tar -zxvf [archive.tar.gz] [path to file]

Remember, you will have to provide the full path to the file you want to extract. You can find the full path of a file or directory with the command tar -tvf [archive.tar].

To extract the test1.txt file from the test.tar and test.tar.gz files, the commands would be:

tar -xvf test.tar test1.txt tar -zxvf test.tar.gz test1.txt

In there:

  1. -x is used to extract files from the archive
  2. -v is used to see the progress as they are extracted
  3. -f is used to specify the tarball name
  4. -z is used to extract the file TAR.GZ

These commands will extract the specified file in the current terminal directory.

Picture 2 of How to extract a file or folder from a TAR or TAR.GZ file

Extract a folder from Tarball

Similarly, you can also extract a directory from a tarball using the following syntax:

tar xvf [archive.tar] [path to directory] tar -zxvf [archive.tar.gz] [path to directory]

For instance, to extract the entire test1 subdirectory from the test.tar archive, you need to provide the full path of the directory, i.e. test/test1:

tar -xvf test.tar test/test1

This will unzip the entire test/test1 subdirectory in the current terminal directory.

Picture 3 of How to extract a file or folder from a TAR or TAR.GZ file

Extract a file or folder to another folder

You can also extract a file or folder from the tarball to another folder. To do this, use the same syntax as above but add the -C option followed by the target directory:

tar -xvf [archive.tar] -C [destination] [file-or-directory] tar -zxvf [archive.tar.gz] -C [destination] [file-or-directory]

Suppose you want to extract a test2 folder from the test.tar archive to the Downloads folder instead of the current working directory. The command, in this case, would be:

tar -xvf test.tar -C ~/Downloads/ test/test2

Picture 4 of How to extract a file or folder from a TAR or TAR.GZ file

Delete a file or folder from Tarball

If you need to delete a file or directory from a TAR or TAR.GZ file, use the --delete option with the tar command:

tar -vf [archive.tar] --delete [file-or-directory]

However, you cannot delete files or folders directly from the compressed tarball (TAR.GZ). What you need to do first is to extract the TAR.GZ file, delete the file or folder and then extract it again.

To extract the TAR.GZ file, use the following command:

gzip -d [archive.tar.gz]

Extracting it will convert the file to TAR. You can now delete files from the TAR archive using:

tar -vf [archive.tar] --delete [file-or-directory]

Then extract the TAR file with gzip:

gzip -f [archive.tar]

Picture 5 of How to extract a file or folder from a TAR or TAR.GZ file

Extracting only the necessary files from an archive not only prevents clutter, but also saves time that would otherwise be spent searching between a large number of files.

Sometimes creating and decompressing TAR files leads to duplicate files in the system. Therefore, you should periodically identify and remove those duplicates to clean up your space.

Update 14 April 2023
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile