Linux Question

Pinpoint archive file from a list of archive files where the target file is zipped

I have a directory structure like this -

./Archive1
./Archive1/Archive2
./Archive1/Archive3


In each directory there are many
tar
files. Say for example (don't go with the name, they are just for example) -

Archive1
├── Archive2
│ ├── tenth.tar.gz
│ └── third.tar.gz
├── Archive3
│ ├── fourth.tar.gz
│ └── sixth.tar.gz
├── fifth.tar.gz
├── first.tar.gz
└── second.tar.gz


Now I have a file
file.txt
, that could reside in any tar file. I need a command that would give me the output as which tar file have my input file (
file.txt
) and also the absolute path of the tar file.

So for example if
test.txt
is in
sixth.tar.gz
. The output will be
sixth.tar.gz
and
./Archive1/Archive3/


Currently I have this command, but the drawback of the command is, it is listing all the tar files -

find . -maxdepth "3" -type f -name "*.tar.gz" -printf [%f]\\n -exec tar -tf {} \; | grep -iE "[\[]|file.txt"

Answer

For each tar file, you can run tar | grep, and if there is a match, print the tar file's name. One way to do this is by running a shell command for each tar file. For a small number of files, and if performance is not too important, this might be good enough and it's fairly straightforward.

find . -maxdepth "3" -type f -name "*.tar.gz" -exec sh -c 'tar tf {} | grep -iEq "[\[]|file.txt" && echo {}' \;

So for example if test.txt is in sixth.tar.gz, the output will be ./Archive1/Archive3/sixth.tar.gz.