Find files in tar archives and extract specific files from tar archives
Published: 17-10-2018 | Author: Remy van Elst | Text only version of this article
This is a small tip, to find specific files in tar archives and how to extract those specific files from said archive. Usefull when you have a 2 GB large tar file with millions of small files, and you need just one.
Finding files in tar archives
Using the command line flags
-ft (long flags are
--file --list) we can list
the contents of an archive. Using
grep you can search that list for the
correct file. Example:
tar -ft large_file.tar.gz | grep "the-filename-you-want"
With a modern tar on modern linux you can omit the flags for compressed archives
and just pass a
.tar.bz2 file directly.
Extracting one file from a tar archive
When extracting a tar archive, you can specify the filename of the file you want (full path, use the command above to find it), as the second command line option. Example:
tar -xf large_file.tar.gz "full-path/to-the-file/in-the-archive/the-filename-you-want"
It might just take a long time, at least for my 2 GB file it took a while.
An alternative is to use "mc" (midnight commander), which can open archive files just a a local folder.Tags: archive , bash , grep , shell , snippets , tar