16

Is it possible to use the find command so that it searches the files within a tar.gz archive also using wildcards?

like

find archive.tar.gz --name *foo*

4 Answers 4

29

How about just:

$ tar tf archive.tar.gz | grep foo
7
  • doesnt that just list all the files?
    – clamp
    Commented Mar 12, 2012 at 13:06
  • @clamp Yes, if you want to do something to each file, simply pipe that output to another command.
    – user13742
    Commented Mar 12, 2012 at 13:07
  • thanks! sorry but im a newbie in unix. i tried: tar tf graphics.tar.gz | find . --name *space* but it says unkown predicate --name
    – clamp
    Commented Mar 12, 2012 at 13:17
  • @clamp find arguments only take one dash, but because it's a list of files on stdin, you want to use grep instead, like hesse's example code. Try tar tf graphics.tar.gz | grep 'space'
    – Kevin
    Commented Mar 12, 2012 at 13:24
  • 1
    grep *foo* isn't a valid regular expression. greg doesn't understand globs, it understands regular expressions. Try grep foo instead.
    – Alexios
    Commented Mar 12, 2012 at 14:19
3

Mount the archive as a directory. You can do it with the AVFS filesystem, which allows you to access any archive as a directory whose name has a trailing #.

mountavfs
cd ~/.avfs$PWD
find archive.tar.gz\# -name '*foo*'
2
  • not work for gzip
    – van abel
    Commented Oct 24, 2018 at 10:19
  • 1
    @vanabel A gzip file doesn't contain other files. It's just one file, compressed. So there's no “inside” to search in. Commented Oct 24, 2018 at 18:10
2

I think it works with no pipe and grep.

like this: tar tvf archive.tar.gz "foo"

as this way, globs will also work.

tar tvf archive.tar.gz "*.foo"

0
0

I made zfind to do this for any archive (zip/tar/rar/7z).

Search inside a single archive:

zfind 'name like "%foo%"' archive.tar.gz

Search for a file in any of the archives on your filesystem:

zfind 'name like "%foo%" and archive'

Or show matches whether they are in- or outside of an archive:

zfind 'name like "%foo%"'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .