BASH :: find file in archive from command line
i know how to find file with find
# find /root/directory/to/search -name 'fil开发者_如何学JAVAename.*'
but, how to look also into archives, as file can be ziped inside...
thanx
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 \;
find /directory -name '*.tgz' -exec tar ztf {} \| grep filename \;
or something like that... But I don't think there's an 'easy' solution.
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.
精彩评论