1

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

  • 1
    You can find answer to your question here http://askubuntu.com/questions/392885/how-can-i-view-the-contents-of-tar-gz-file-without-extracting-from-the-command-l – Ejaz Sep 05 '16 at 00:21
  • @Ejaz please read my question again , i want to "find all tar.gz" file then looking inside all of them ! – user3325376 Sep 05 '16 at 00:34
  • You want to process all *.tar.gz files in your computer and list those containing files/directories whose names match your search pattern? – Byte Commander Sep 05 '16 at 01:00
  • I think he's trying to search for a file with a given filename within all the tar.gz files on his computer. A combination of commands in my opinion, and best done with a custom script. – anonymous2 Sep 05 '16 at 02:07

1 Answers1

2

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
Byte Commander
  • 107,489