[Novalug] grep question

James Ewing Cottrell 3rd JECottrell3@Comcast.NET
Fri May 21 11:24:05 EDT 2010


OK, there are three ways to deal with symlinks.

[1] Give the user a chance to choose
[2] Never Follow
[3] Always Follow

Clearly [1] is the best solution. But [2] should be the default.

To use a bit of Perl terminology, the Proper Way to deal with symlinks 
is to Follow in Scalar (non-recursive) Context and to Not Follow them in 
List (Recursive) Context.

Otherwise you can create Loops. And that situation is Intolerable.

But even if loops are not involved, the results can be disasterous. One 
day I added a symlink from our source tree to our distro repo, and grep 
-r suddenly became unusable.

Just like the original cp -r was unusable in 4.3 BSD because of this 
mistake. BTW, GNU fixed this when they rewrote cp.

So here we have two cases where the authors of a feature wanted behavior 
[3], without thinking thru the ramifications. Of the three 
possibilities, they made the worst choice.

Not only did they Repeat Themselves by Failing To Learn From History, 
they Failed To Learn From The Present.

So...either hack in the requisite follow/don't follow options, or remove 
-r. If you really want recursive grep behavior, hack up an rgrep shell 
function or script using find find and grep.

JIM



Michael Henry wrote:
> On 05/20/2010 12:45 PM, James Ewing Cottrell 3rd wrote:
>> "grep -r pattern dirs..." is roughly equivalent to
>>
>> find dirs... -type f | xargs grep pattern or
>> find dirs... -type f  -exec grep pattern +
>>
>> except for the egregious mistake of following symlinks.
> 
> I was surprised by this statement, since when I'm interactively
> searching down a directory tree, I generally *want* to follow
> symlinks.  In my view, the purpose of a symlink to a file is to
> make that file appear to be in the containing directory.  I'm
> pleased that other tools follow symlinks transparently (e.g.,
> ``cat some-symlink``), so I'm wondering why it seems like a
> mistake to you.  I do understand and appreciate why ``find``
> doesn't follow symlinks, though, since its purpose is to permit
> fine-grained selectivity across a tree in the filesystem, and
> it's important to differentiate symlinks from hard links in that
> case.  But for tools intended to read files, I prefer them to
> follow symlinks by default, and I'm curious as to your reasoning
> to consider that behavior an egregious mistake.
> 
> Michael Henry
> 
> 
> ------------------------------------------------------------------------
> 
> 
> No virus found in this incoming message.
> Checked by AVG - www.avg.com 
> Version: 9.0.819 / Virus Database: 271.1.1/2886 - Release Date: 05/20/10 14:26:00
> 



More information about the Novalug mailing list