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

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.

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

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.

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

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.

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

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

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

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]

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

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.

4 ★ | 1 Vote

May be interested

  • How to Unzip a FileHow to Unzip a File
    this wikihow teaches you how to extract (or 'unzip') a zip folder's contents. extracting files from a zip folder will decompress the files, allowing you to open and run them properly. you can easily unzip zip folders by using the built-in...
  • 3 Simple way to extract ISO file3 Simple way to extract ISO file
    3 simple way to extract iso file. iso files are a popular file format, especially for large files such as windows, office installers, or heavy game installation files. extracting iso file is quite simple, if you do not know how
  • How to Extract a JAR FileHow to Extract a JAR File
    this wikihow teaches you how to extract a jar file's contents. jar files are zipped java files that are typically executed within a java environment. while extracting jar files can be tricky depending on your computer's operating system,...
  • How to extract ZIP file on computer quicklyHow to extract ZIP file on computer quickly
    how to extract zip file on computer quickly. if you regularly download the data files on the internet to your computer, it is probably no stranger to the zip file format. this is a very common file compression format, compressing the file will ensure t
  • How to Open a Zip FileHow to Open a Zip File
    this wikihow teaches you how to open and extract a zip folder on your computer, smartphone, or tablet. zip folders are used to compress files into smaller versions, making them easier to store and transport. in order to view and use a zip...
  • How to Set Password for Folder and File in WindowsHow to Set Password for Folder and File in Windows
    you have some important documents in an important folder or file that you don't want others to see. the following article will show you how to encrypt or create a password to protect a folder or file to ensure that no one will be able to open it but you and the recipient.
  • How to Zip Folder on WindowsHow to Zip Folder on Windows
    this wikihow teaches you how to create a zip file from a folder using either the built-in windows zip tool or a third-party app like winzip. navigate to the folder you want to zip. no need to open the folder, just bring it onto the screen....
  • Instructions to change DMG file to ISO fileInstructions to change DMG file to ISO file
    dmg image file format is the most common file storage format used to distribute software on mac os x. however, windows pc cannot read this file. therefore, to extract the contents of the dmg file on a computer running windows or when you want to burn the image file to disk, you first need to convert the file to an iso image file. here's how to convert a dmg file into an iso file that can be extracted on a windows pc.
  • The best RAR file extraction application on AndroidThe best RAR file extraction application on Android
    rar format is a popular file type for data compression. although you can use rar files on macos and windows more on your phone, sometimes you still need the tool to work with it on the move. check out the best rar file extraction application on android.
  • How to save files to multiple folders on the web browserHow to save files to multiple folders on the web browser
    if you're someone who likes to organize data files, you're probably quite annoyed at how chrome and firefox handle the download file. you have a folder for videos, gif images, jpg images, important documents, etc. however, when downloading files you can only save them to the downloads folder or navigate to a specific folder using the save as dialog box.