Home > Software engineering >  Make tar stop searching for file after first hit
Make tar stop searching for file after first hit

Time:12-19

I have a big file with a long list of files inside a tarball:

subdir1/file1.txt
subdir2/file2.txt

I want to extract these files from a tarball by using a while loop, like this:

while read i
do
tar -xvf tarball.tar.gz $i
done < filelist.txt

However, due to the size of the tarball and the filelist, this has a very long runtime because tar is searching through the whole tarball even after finding the file in question.

Is there a way to make tar stop searching after the first hit? I have tried using the --occurrence 1 option described here, but that did not seem to do the trick. I am using tar version 1.26.

CodePudding user response:

Does your version support this option?:

-q, --fast-read (x and t mode only) Extract or list only the first archive entry that matches each pattern or filename operand. Exit as soon as each specified pattern or filename has been matched. By default, the archive is always read to the very end, since there can be multiple entries with the same name and, by convention, later entries overwrite earlier entries. This option is provided as a performance optimization.

CodePudding user response:

tar x can be given multiple filenames to extract in one run instead of using a loop:

readarray -t files <filelist.txt
tar xzvf tarball.tar.gz "${files[@]}"

GNU tar supports a -T argument for getting filenames to add or extract directly from another file:

tar -xzvf tarball.tar.gz -T filelist.txt
  • Related