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: 03/05/05


Date: Sat, 05 Mar 2005 09:56:57 GMT

jenea wrote:

> 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
"Program", "Files").

"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
-print0" bit).

   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?



Relevant Pages

  • Re: simpletest vs phpunit vs ...
    ... How do I use grep command in Linux and Unix like operating systems? ...
    (comp.lang.php)
  • Re: find - exec vs xargs
    ... If I use exec grep it takes way longer ... to complete than xargs grep. ... It is likely that the time of this command is dominated by the ... outputs a list of file names separated by NL characters while NL ...
    (comp.unix.shell)
  • SUMMARY - find -exec vs xargs
    ... grep working on a single file. ... bunches to command, so that command is run fewer times (only once, in ... So, if we use find | xargs grep ..., the find is able to ... There isn't a forked process for every single file. ...
    (SunManagers)
  • Re: MacTeX 2007 and documentation
    ... may be the most useful command available when doing searches at the ... will find every file on your system that has the text "memoir" on it, ... If locate generates 30 lines of text, where each line is a file, grep ... I need a Unix guru to chat to for half an hour or so to put me straight. ...
    (comp.text.tex)
  • Re: appending syslog output to file
    ... I cannot find a postrotate function. ... 'postrotate' is a "command" in the logrotate ... grep mystring syslog.0>>myfile ... grep $NBR numbers ...
    (uk.comp.os.linux)