How to extract text data from .tar/.tgz/folder/folder/.txt

I have a large .tar file which has many .tgz folders in it. And inside each .tgz folders there are many folders and folders and inside it there is .txt file. I want to extract .txt file and nothing else. How to do it. Please help

1 answer

  • answered 2020-01-17 06:15 shuvalov

    Iterate over list of the files and extract only what you need:

    bundle=$1 # large .tar filename
    for tgz in $(tar -tvf $bundle |  awk  '{ if($NF != "") print $NF }')
    do
        for file in $(tar -xOf $bundle "$tgz" | tar -tvf - | awk  '{ if($NF != "") print $NF }' | grep "\.txt$" )
        do
            tar -xOf $bundle "$tgz" | tar -xf - $file
        done
    done