0

This question already has an answer here:

I have lots of tar(.tar.bz2) files containing multiple file types in a recursive directory. I want to extract just one type of file(let's say .txt files) from all the directories. How do i do that? I have this command to extract all the files in each directory:

for file in *.tar.bz2; do tar -jxf "${file}"; done

I want to extract only the ".txt" files instead of all.

marked as duplicate by αғsнιη, Anthony Geoghegan, Romeo Ninov, GAD3R, Timothy Martin Apr 24 '18 at 21:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I believe my question is different from the question in the link. I have to extract the specific files recursively. – George Apr 24 '18 at 12:11
2

Quoting the GNU tar manpage:

Thus, to extract files whose names end in `.c', you can use:

$ tar -xf foo.tar -v --wildcards '*.c'

So for you case, I'd go with:

for file in *.tar.bz2; do tar -jxf "${file}" --wildcards '*.txt'; done
1

With bsdtar, you can specify a glob:

for file in *.tar.bz2; do tar -jxf "$file" '*.txt'; done

With gnutar, you need to add the flag --wildcards:

for file in *.tar.bz2; do tar -jxf "$file" --wildcards '*.txt'; done

Not the answer you're looking for? Browse other questions tagged or ask your own question.