i have a lot's of tar.gz files in random place and i want to look for a file inside all of tar.gz file
for example :
/root/a.tar.gz
/home/backup-2016.tar.gz
/home2/user/files/2015/abc.tar.gz
i am looking for file or folder inside of them
i have a lot's of tar.gz files in random place and i want to look for a file inside all of tar.gz file
for example :
/root/a.tar.gz
/home/backup-2016.tar.gz
/home2/user/files/2015/abc.tar.gz
i am looking for file or folder inside of them
You can use this (rather long) one-liner, replacing PATTERN
with your search pattern (case insensitive unless you remove the -i
option after grep
):
find / -iname "*.tar.gz" -exec bash -c 'result=$(set -o pipefail ; echo "{}" ; tar -tf {} | grep -i "PATTERN" | sed "s/^/ /") ; test $? -eq 0 && printf "$result\n"' \;
If you don't want to search your whole computer but just a specific directory (recursively), you may specify that as first argument after find
instead of the /
. The .tar.gz
suffix for archives to process is case insensitive as well unless you replace the -iname
after find
with name
.
The output might look somehow like this (searching in /usr/share/doc
for the pattern default
):
/usr/share/doc/openjdk-8-jre-headless/test-amd64/failed_tests-hotspot.tar.gz
test/langtools/JTwork/tools/javac/lambda/methodReferenceExecution/MethodReferenceTestInnerDefault.jtr
test/langtools/JTwork/tools/javac/lambda/methodReferenceExecution/MethodReferenceTestSuperDefault.jtr
test/langtools/JTwork/tools/javac/lambda/methodReferenceExecution/MethodReferenceTestVarArgsSuperDefault.jtr
test/langtools/JTwork/tools/javac/lambdaShapes/org/openjdk/tests/vm/DefaultMethodsTest.jtr
/usr/share/doc/apg/php.tar.gz
./themes/default.php
You see the archive path in one line followed by all content entries matching the pattern, each in a separate line indented by 4 spaces.
This can be also put in a script (e.g. archive-search
, don't forget to make it executable with chmod +x FILENAME
):
#!/bin/bash
# Usage: archive-search PATTERN DIRECTORY
find "$2" -iname "*.tar.gz" -exec bash -c '
result=$(
set -o pipefail
echo "{}"
tar -tf {} | grep -i "$0" | sed "s/^/ /"
)
test $? -eq 0 && printf "$result\n"
' "$1" \;
or in a Bash function (e.g. archive-search
, append to your ~/.bashrc
if you want to have it available in every new Bash session):
archive-search () {
# Usage: archive-search PATTERN DIRECTORY
find "$2" -iname "*.tar.gz" -exec bash -c '
result=$(
set -o pipefail
echo "{}"
tar -tf {} | grep -i "$0" | sed "s/^/ /"
)
test $? -eq 0 && printf "$result\n"
' "$1" \;
}
Both the script and the function take search pattern and directory as command-line arguments, like this:
archive-search "default" /usr/share/doc