0

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.

George
  • 3
  • 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 Answers2

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
terdon
  • 242,166
Tensibai
  • 183
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