-1

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.

Minh Ha Pham
  • 121
  • 6

2 Answers2

0

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!

Castaglia
  • 3,349
  • 3
  • 21
  • 42
0

I recommend you to create a small script for that task. Something like the following:

REGEX="my search string"

find . -type f -name '*.tar' -print0 | while read -r -d $'\0' FILE
do
  # the sed command adds the filename prefix
  tar tf "${FILE}" | grep -- "${REGEX}" | sed "s|^|${FILE}: |"
done
zuazo
  • 176
  • 5