Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 5 Jan 2002 16:11:24 -0500
From:      Leo Bicknell <bicknell@ufp.org>
To:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: Overriding ARG_MAX
Message-ID:  <20020105211124.GA76316@ussenterprise.ufp.org>
In-Reply-To: <200201052048.g05KmeW55815@lurza.secnetix.de>
References:  <Pine.BSF.4.21.0201042248520.27496-100000@search.sparks.net> <200201052048.g05KmeW55815@lurza.secnetix.de>

next in thread | previous in thread | raw e-mail | index | archive | help

Ok, there are several issues here that I just have to point out. :-)

In a message written on Sat, Jan 05, 2002 at 09:48:40PM +0100, Oliver Fromme wrote:
> ls | grep '\.out$' | wc -l

One process shorter: find -name "*.out" -maxdepth 0 | wc -l

> ls | grep '\.data$' | xargs grep something

Two problems, first use find, second use /dev/null:

find -name "*.data" -maxdepth 0 | xargs grep something /dev/null 

> ls | grep '\.foo$' | xargs cat | grep somethingA

A completely unnecessary use of cat.


One of the often missed things is the use of /dev/null on grep in
this case.  If you grep a single file ("grep string file") then it
prints the matches.  If you grep multiple files ("grep string file1
file2") it prints the matches with the file name prepended.

When using xargs, if the number of things to search results in just
one being left for the last call to grep you will get all of your
results prepended with the file name, except for the last file
which will be just the results.  Adding /dev/null insures grep
always has 2 or more files.

Another fix would be:

find -name "*.data" -maxdepth 0 | xargs grep -H something

I don't believe old grep's had -H though, which is where the /dev/null
trick came from.

In any event, using find is much better, not so much for this example,
but because it allows you to do things like check permissions in a 
portable way:

find -name "*.data" -perm 444 | xargs grep -H something

You can't do that with ls | grep, since only the filenames make it
to grep.

-- 
       Leo Bicknell - bicknell@ufp.org - CCIE 3440
        PGP keys at http://www.ufp.org/~bicknell/
Read TMBG List - tmbg-list-request@tmbg.org, www.tmbg.org

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020105211124.GA76316>