Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH :: find file in archive from command line

Tags:

bash

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

like image 914
darkoni Avatar asked Nov 22 '25 14:11

darkoni


1 Answers

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 \;
like image 141
wishi Avatar answered Nov 24 '25 03:11

wishi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!