0

The command I'm trying (as I'm trying to find tars with 'butterflyinmotion.com' as a dir in them) is:

find / -name "*.tar.gz" -exec tar -tzf {} \; | grep -W butterflyinmotion

This shows a lot of errors and isn't working. The grep -W is necessary so I know what file the butterflyinmotion.com backup is in. Thanks!

3
  • If you are using gnu grep, then there is no -W option which is at least one of the reasons that you are receiving errors. If you just want the filename that the string appears in, use the -l option. If you want both the string and the filename, use the -H option.Lastly, add the expected output to your question. – Nasir Riley Feb 9 '20 at 22:38
  • There is an option to show filenames (either -l or -L) for grep, but that work because all grep see is a stream of lines. – Baard Kopperud Feb 9 '20 at 23:16
  • It may be a good idea to add 2> /dev/null between \; and | grep, to remove all those "access denied" errors. – Baard Kopperud Feb 9 '20 at 23:34
0

I think it would be easier to make a little script, and call it with find -exec.

For example:

#!/bin/sh
echo ">>> $1"
tar tvf "$1"

call it with -exec ./script.sh "{}" \; and redirect the result to file, then grep the file egrep '>>>|WhatYouLookFor' file

You could also grep in the script:

#!/bin/sh
echo ">>> $1"
tar tvf "$1" | grep 'WhatYouLookFor'

Or more flexible:

#!/bin/sh
echo ">>> $2"
tar tvf "$2" | grep "$1"

call it with -exec ./script.sh 'WhatYouLookFor' "{}" \;

0

At least with GNU grep, you could do something like this:

find / -name '*.tar.gz' -exec sh -c '
  for t; do 
    tar -tvf "$t" | grep -m1 -l --label="$t" "butterflyinmotion"
  done
' {} + 2>/dev/null

This will output the names of the .tar.gz files whose archives contain at least one match of the pattern butterflyinmotion


With GNU tar, it appears to be possible (at least for simple patterns) to omit the grep and use the exit status of tar -tf directly ex.

find / -name '*.tar.gz' -exec tar --wildcards -tf {} '*butterflyinmotion' \; -print 2>/dev/null

The above will print the matching files (if any) followed by the name of the archive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.