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
Table of Contents
This is a small tip, to find specific files in tar archives and how to extractthose specific files from said archive. Usefull when you have a 2 GB large tarfile 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 listthe contents of an archive. Using
grep you can search that list for thecorrect 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 archivesand 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 lineoption. 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 filesjust a a local folder.Tags: archive, bash, grep, shell, snippets, tar