0

I tried this:

find /usr/lib -print0 | grep zip | xargs -0 -I{} echo "found file: {}"
find /usr/lib -print0 | grep --null zip | xargs -0 -I{} echo "found file: {}"

But it doesn't work because grep only says there is a binary file matching. I want grep to output null terminated lines.

Is it possible to fix this without changing the whole command? I know it's possible to use find -name ... -exec .... But it would be nice if my existing command could be fixed instead.

zomega
  • 972
  • Just a suggestion: have you tried fdfind, I find it much easier to use... excuse the pun. – a2k42 Mar 08 '23 at 09:49

1 Answers1

6

Given the use of --null, I’m assuming you’re using GNU grep. You can tell it to consider the data as null-separated with the -z (--null-data) option, and to treat everything as text with the -a option:

find /usr/lib -print0 |
  grep -a -z zip |
  xargs -r0 printf 'found file: %s\n'

(remember you can't use echo to output arbitrary data).

--null only affect’s grep’s output of file names, and has no effect here.

As you mention, you can do this entirely using GNU find, without even using -exec, though you need LC_ALL=C to be able to find files whose path has non-text on either side of zip (unlikely in /usr/lib though):

LC_ALL=C find /usr/lib -path '*zip*' -printf 'found file: %p\n'
Stephen Kitt
  • 434,908