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.
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:
- -x is used to extract files from the archive
- -v is used to see the progress as they are extracted
- -f is used to specify the tarball name
- -z is used to extract the file TAR.GZ
These commands will extract the specified file in the current terminal directory.
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 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
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]
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.
You should read it
- How to extract IMG files in Windows 10
- How to extract Zip files on your computer
- Unzip Zip, RAR, ... files online without software
- Extract images from PDF files
- How to Extract a JAR File
- How to extract multiple files on Windows 10
- How to safely extract RAR files on MacBook
- How to use ZArchiver to compress and decompress files on Android
- How to quickly extract .tar.gz file on Windows 10
- How to open and extract ISO files with Winrar
- How to Extract ZIP Files
- How to use Data Miner to extract data from websites
Maybe you are interested
Europe develops satellite internet system to replace Starlink
Compare the size of the Earth with the Sun and the largest star known to man
BadBox Malware Is Picking Up Speed, Targeting Certain Android Devices
How to Change the Default User Account Avatar in Windows
Astronomers discover first celestial sphere around a sun-like star
How to Avoid Post-Tracking Targeted Ads After Shopping Online