0

grep's man page reads

"grep -lZ outputs a zero byte after each file name instead of the usual newline"

But in my case there are files in the output that still need quotes. In order for this to work:

/usr/bin/grep -rlZ 'Not Found'|xargs -0 rm

the -w in a filename like -webkit-stuff.html should not be interpreted as an option to rm. So I'm currently doing this:

/usr/bin/grep -rlZ 'Not Found'|\
xargs -0 -I{} find . -type f -name {} -print0 | xargs -0 rm

That's not efficient. Is there a grep version that has "a better -print0"?

How would you do this?

  • I really don't think this is anything to do with quoting - your solution works because find prefixes the filenames with the starting-point under which it was found (in this case, ./) so that names like -webkit-stuff.html become ./-webkit-stuff.html – steeldriver Aug 2 '18 at 23:32
4

You could use

/usr/bin/grep -rlZ 'Not Found' | xargs -0 -r rm --

where the -- stops the utility from interpreting anything afterwards as an option, or,

/usr/bin/grep -rlZ 'Not Found' . | xargs -0 -r rm

which would cause grep to prefix the pathnames of all files with ./. The -r option to xargs makes xargs not run the command if it gets no input from grep (i.e. if no files contained the string).

I would personally probably have used find though:

find . -type f -exec grep -qF 'Not found' {} ';' -delete

(which I think does the same thing: recursively deleting files that contains the string Not found)

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.