Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Extract a File or Folder from a TAR or Tar.gz File

Learn about TAR, including View the Content of Tarball, key uses, practical steps, common issues, and answers to frequently asked questions.

Table of Contents

This practical guide explains how to extract a file or folder from a tar or tar.gz file with clear steps and useful context. It also covers common requirements, potential problems, and the details that can help you get better results.

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.

View the Content of Tarball - TAR

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.

Extract a File from Tarball - TAR

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.

Extract a Folder from Tarball - TAR

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

Extract a File or Folder to Another Folder - TAR

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]

Delete a File or Folder from Tarball - TAR

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.

Conclusion

Understanding TAR makes it easier to compare options, avoid common mistakes, and apply the information in this guide more effectively. Review the relevant requirements before making changes or choosing a solution.

FAQ

How do you extract a file or folder from a tar or tar.gz file?

Follow the steps in this guide in order, confirm the required settings or tools, and verify the result before making additional changes.

Is it safe to extract a file or folder from a tar or tar.gz file?

It is generally safe when you follow the recommended instructions, use trusted tools, and avoid changing settings you do not understand.

What should you do if the process does not work?

Recheck the requirements, restart the device or application when appropriate, and review each step for missed settings or compatibility issues.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.