I need a way to run grep on a list of files which may contain spaces in the file name. On a list of files with no spaces, it's pretty straight forward and easy. Also, I know how to deal with spaces in the file names for find and xargs. I'm just looking for a way to use grep
with the -print0 command.
I'm looking for text inside the file, not in the filename…
This lists all files:
find * -print 0 | xargs -0 -i{} echo {}
but say I have to do a grep (or some other command) inbetween.
This lists files which contain "howdy doody" inside the file:
find * | xargs grep -l "howdy doody" | xargs -i{} echo {}
This doesn't work, grep
doesn't know how to recognize null terminated lines. Did I miss something in grep
's man page?
find * -print0 | xargs grep -l "howdy doody" | xargs -0 -i{} echo {}
Best Answer
Are you looking for "howdy doody" in the filename, or within the file?
xargs
is what you need to use to split the null-terminated output fromfind -print0
. In your example, theecho
is extraneous; don't bother with it.You can also run your command directly from the
find
with-exec
. The difference is thatfind -exec
runs the command once per file found;xargs
adds filenames to the end of the command (up to system limit), so it runs the command fewer times. You can simulate this withxargs -n1
to forcexargs
to only run one command per input.