tags:

views:

78

answers:

3

i know how to find file with find

# find /root/directory/to/search -name 'filename.*'

but, how to look also into archives, as file can be ziped inside...

thanx

A: 

find /directory -name '*.tgz' -exec tar ztf {} \| grep filename \; or something like that... But I don't think there's an 'easy' solution.

Wim
+1  A: 

I defined a function (zsh, minor changes -> BaSh)

## preview archives before extraction
# Usage:     show-archive <archive>
# Description:  view archive without unpack
  show-archive() {
        if [[ -f $1 ]]
        then
                case $1 in
                        *.tar.gz)      gunzip -c $1 | tar -tf - -- ;;
                        *.tar)         tar -tf $1 ;;
                        *.tgz)         tar -ztf $1 ;;
                        *.zip)         unzip -l $1 ;;
                        *.bz2)         bzless $1 ;;
                        *)             echo "'$1' Error. Please go away" ;;
                esac
        else
                echo "'$1' is not a valid archive"
        fi
  }

You can

find /directory -name '*.tgz' -exec show-archive {} \| grep filename \;
wishi
Looks nice. Some comments: why gunzip+tar for .tar.gz, but tar -zt for .tgz? They're both the same anyway. bzless also doesn't do too well inside scripts ;-) And probably the file will be a tar.bz2 anyway, so you really need tar -jt
Wim
BTW this will only show matching files by their path *inside* the archive, you won't know the archive's name this way.
Wim
can I suggest using the `file` command to determine the archive type rather than the file extension
David Dean
A: 

If your archive is some sort of zipped tarball, you can use the feature of tar that searches for a particular file and prints only that filename. If your tar supports wildcards, you can use those too. For example, on my system:

tar tf sprt12823.logs.tar --wildcards *tomcat*

prints:

tomcat.log.20090105

although there are many more files in the tarball, but only one matching the pattern "*tomcat*". This way you don't have to use grep.

You can combine this with find and gunzip or whatever other zipping utility you've used.

shoover