Find all files which contain pattern in directory of tar files

I have all tar files in a directory. I want to find some files in tar files without extract all tar files.

I know how to work with one tar file:

tar tvf abc.tar | grep xyz

I want to find in all tar files but I don’t know how to.

Answer

Let’s assume that all of your tar filenames end in .tar. And let’s assume that you’re looking for any file, within all of those tar files, whose name ends in .dat. With these assumptions, you might then use:

$ for f in `find . -type f -name "*.tar" -print`; do tar tf $f | grep \.dat > /dev/null && echo $f; done

The find command is used to get the list of .tar files; you might end up with directories that contain files other than .tar files. For each .tar file found, list its table of contents (tar tf), and grep that table of contents for names matching our pattern.

The example command above simply prints out the names of the .tar files which contain files matching our pattern; if you also wanted to see the names of matched files, then you might remove the > /dev/null part of the grep command.

Hope this helps!

Attribution
Source : Link , Question Author : Minh Ha Pham , Answer Author : Castaglia

Leave a Comment