Re: How to search through all the files in all the levels of your directories.
From: Robert Newson (ReapNewsB_at_bullet3.fsnet.oc.ku)
Date: Sat, 05 Mar 2005 09:56:57 GMT
> just out of curiosty....
> what does this cryptic command do:
> find -type f -print0 |xargs -0 grep -i foo
> why is there placed xargs , grep -i , -print0....
> I figured out what each of them mean... but when putted this all
> together it gets messy for me....
If you try to:
grep -i foo `find -type f`
you may find that the list of files created by "find" creates an argument
list to "grep" that is too long for the system to handle. This pipeline
gets round this problem:
"-print0" means follow each file name output by a \0 (nul) character as
opposed to a space - that way any file names with spaces in them (eg
"Program Files") are treated as a single name (and not two or more - eg
"xargs -0" means the separator between each input item is a \0 (nul)
character (as opposed to white(?)space). Thus "Program Files" would be sent
as "Program Files\0" and read as one arg, as opposed to being sent as
"Program Files" and being treated as 2 args "Program" and "Files".
"grep -i foo" is the command that xargs is going to run with the args
supplied on its stdin.
So what we have is this:
Search the filing tree for files of type "f" (regular files) and list
them to stdout, separating each with a nul (\0) character (the "find -type f
Send this output to the stdin (input) of xargs (the "| xargs" bit).
Xargs then reads from its stdin a list, separated by a nul (\0) character
(the "-0" bit of "xargs -0 ...") and uses them as the arguments to the
command given as its argument (the "grep -i foo" bit).
However, xargs takes into account the system limits on how long an
argument list can be and so can generate one or more instances of the
command given as its argument, each with a (non overlapping) subset of the
list that was presented to the stdin of xargs.
Thus "grep -i foo" gets a list of files to check in its arguments found
from the filing tree (by "find"), but if there are too many files which
would create an argument list too long for the system to handle, the first n
files will go to one grep command, then the next n+1 to m files go to
another grep command, etc; until the list of files found has been exhausted.
I hope that clears it up?