Web lists-archives.com

Re: An appropriate directory search tool?

On Sat, Oct 20, 2018 at 05:28:52AM -0500, Richard Owlett wrote:
> But they did.
> That's why I wrote 'My take away from answers so far is "A script will be
> required." '
> Perhaps we have different ideas of the definition of "script".
> I saw the examples which worked as scripts (even if written as one liners).
> If I had attempted to use bash, I would have expected to use an explicit
> pipe command between 'find' and 'grep'.
While you can certainly do that, the authors find were clearly aware
that the "find one or more files based on some criteria and then perform
one or more operations on those files" would be a common enough workflow
that they included the -exec and -execdir options to find.  With those
you do not need a pipe between find and grep.  It also effectively deals
with the situation where the file list is so long that you exceed the
system limit for the length of a command line.  The -exec option will
perform the specified action once per file found that meets the
criteria, where the pipe approach would pass the entire output of find
into the grep command.  That is sometimes not what you want.

However, if you still want to use the pipe approach, I would advise you
to look at the -print0 option to find and the -0 option to xargs.  Since
the normal find output is one file name per line and since newlines are
valid in file names, it may sometimes not yield the expected result.  A
NULL character, however, is not valid in a filename, so it is an
effective separator and you can use it like this:

find <dir-or-dirs> <criteria> -print0 | xargs -0 <cmd> <cmd-opts>

That will ensure that you actually get the result you expect and as a
bonus xargs handles the "do this command once for each input line"
dance, so you are not likely to exceed the system limit on the length of
a command line.



Roberto C. Sánchez