Linux Question

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

I have a directory structure like this -


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

├── 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
, 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 (
) and also the absolute path of the tar file.

So for example if
is in
. The output will be

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download