[comp.unix.questions] Finding files

billc@prism.UUCP (12/08/87)

	Right now, to find a file somewhere under my current directory,
	I use the following alias:

	    alias	where	"find \$cwd -name \!* -exec echo {} \;"

	Question: Is there a better way to do this?

jbatson@tron.bbn.com.bbn.com (James Batson) (12/10/87)

In article <205700003@prism> billc@prism.UUCP writes:
>	Right now, to find a file somewhere under my current directory:
>	    alias	where	"find \$cwd -name \!* -exec echo {} \;"
>	Question: Is there a better way to do this?

Why not try
	alias where "find \$cwd -name \!* -print"

The exec is an expensive way to get a filename that find already knows
about, and will print for you.  Is there a reason you were echo'ing
instead of printing?

Jay Batson

bamford@ihlpg.ATT.COM (Harold E. Bamford) (12/10/87)

In article <205700003@prism> billc@prism.UUCP writes:
>
>	Right now, to find a file somewhere under my current directory,
>	I use the following alias:
>
>	    alias	where	"find \$cwd -name \!* -exec echo {} \;"
>
>	Question: Is there a better way to do this?

At the risk of being one of thousands with an alternative:

	alias f="find . -type f -print"

If I want to find a particular file:    f | grep filename

-- 
				Harold Bamford, AT&T Bell Labs
				IHP 2F-524 (312) 416-7397
				ihnp4!ihlpg!bamford

hartzell@boulder.Colorado.EDU (George Hartzell) (12/10/87)

In article <205700003@prism> billc@prism.UUCP writes:
>
>	Right now, to find a file somewhere under my current directory,
>	I use the following alias:
>
>	    alias	where	"find \$cwd -name \!* -exec echo {} \;"
>
>	Question: Is there a better way to do this?

how about:
	alias where "find \$cwd -name \!* -print\;"
g.

George Hartzell			                 (303) 492-4535
MCD Biology, University of Colorado-Boulder, Boulder, CO 80309
hartzell@Boulder.Colorado.EDU  ..!{hao,nbires}!boulder!hartzell

pinkas@cadev4.intel.com (Israel Pinkas ~) (12/10/87)

In article <205700003@prism> billc@prism.UUCP writes:
>
>	Right now, to find a file somewhere under my current directory,
>	I use the following alias:
>
>	    alias	where	"find \$cwd -name \!* -exec echo {} \;"
>
>	Question: Is there a better way to do this?


Try:

alias where 'find . -name \!!:1 -print'

There are a few problems with the alias:

1) csh guarantees to expand all variables inside double quotes.  Thus, cwd
is expanded at alias definition time.  For example:

	echo "\$mail"

returns

	\/usr/spool/mail/pinkas

2) $cwd is not an acurate reresentation of the current directory.  Consider
what happens when you cd to a symbolic link which points to a directory.
$cwd is set to the name of the symbolic link.  Now, if you type 'cd ..',
`pwd` and $cwd return two vastly different things.  I take care of this by
aliasing every command that might result in a directory change to execute
an alias called sprompt, which has a side effect of setting cwd correctly.

3) \!* will return all the arguments given to the alias.  If more than one
is given, this will result in bad parameters to find.

4) -print is more efficient than forking a shell to execute echo.  Has the
same result.

The alias that I gave is one that I use for the csh class that I teach
internally.  About 20-30 of my students have let me know that they use it
all the time.

-Israel

----------------------------------------------------------------------
Disclaimer: The above are my personal opinions, and in no way represent
the opinions of Intel Corporation.  In no way should the above be taken
to be a statement of Intel.

UUCP:	{amdcad,decwrl,hplabs,oliveb,pur-ee,qantel}!intelca!mipos3!cadev4!pinkas
ARPA:	pinkas%cadev4.intel.com@relay.cs.net
CSNET:	pinkas%cadev4.intel.com

---------
"You can do more with a kind word and a gun than with just a kind word"

			-Al Capone

lvc@tut.cis.ohio-state.edu (Lawrence V. Cipriani) (12/10/87)

In article <4441@ihlpg.ATT.COM>, bamford@ihlpg.ATT.COM
	(Harold E. Bamford) writes:
> 
> At the risk of being one of thousands with an alternative:
> 
> 	alias f="find . -type f -print"
> 
> If I want to find a particular file:    f | grep filename
> 

Try the -name option of find:

	find dir -name '*filename*' -a -print

this tends to be faster.

-- 
	Larry Cipriani AT&T Network Systems at
	cbosgd!osu-cis!tut!lvc Ohio State University

fm06222@dlag1.UUCP (Bill Wallace) (12/10/87)

In article <205700003@prism>, billc@prism.UUCP writes:
> 
> 	    alias	where	"find \$cwd -name \!* -exec echo {} \;"
> 
> 	Question: Is there a better way to do this?

On our copy of Unix (Gould UTX/32 - basically a BSD 4.3 clone) we have
the option of establishing a database for find.  There's a script that
we run out of crontab in the middle of the night to update the database
daily, so the results are usually good enough.  The command is simple:

	    find  <string>

and so doesn't really need an alias.  Note, unlike the -name option,
a search is made on any occurrence of the string - not just an exact
match of the name.  This is both useful and frustrating.  A single
"find core" will help the systems administrator clean up disk space,
but you'd better pipe the output from "find alais" to more. (or less)

-- 
*  Bill Wallace     {seismo!gould,cbosgd!osu-cis}!dsacg1!dlag1!bwallace  *
*  Administrative Support Center  (DASC-ZWC)      phone  (202) 274-9336  *
*  Cameron Station  Room 4C652                    Autovon      284-9336  *
*  Alexandria,  VA  22304                                                *

marki@hpiacla.HP.COM (Mark Ikemoto) (12/11/87)

How about...

	alias	where	"find . -name \!^ -print"


Mark

**************************************************************************
* Mark Ikemoto 	            | {ucbvax,hplabs}!hpda!hpiacla!marki  [UUCP] *
* Indus. Appl. Center (IAC) | mark@hpiacla                        [SMTP] *
* Hewlett-Packard Co.       | (408) 746-5453                      [AT&T] *
* 1266 Kifer Road           | 1-746-5453                     [HP-TELNET] *
* Sunnyvale, CA  94086  USA | "What If..."                [HP-TELEPATHY] *
**************************************************************************

jim@xanth.cs.odu.edu (Jim Duncan) (12/11/87)

In article <205700003@prism> billc@prism.UUCP writes:
>	    alias	where	"find \$cwd -name \!* -exec echo {} \;"
>	Question: Is there a better way to do this?

Yes.  Why not use `-print' in place of `-exec echo {} \;' ?  Also, I'm not
sure of what happens if your script comes accross a filename with asterisks
in it -- possible you should escape it also.  Lastly, wouldn't it be better
if you started the find with the current directory instead of your $HOME ?
I assume you're in a different situation than I am -- I have a ridiculous
number of subdirectories, and find's depth-first search would take forever
to locate some of my stuff if I always started at $HOME.



-- 
 Jim Duncan, Computer Science Dept, Old Dominion Univ, Norfolk VA 23529-0162
 (804)440-3915     INET: jim@xanth.cs.odu.edu    UUCP: ...!sun!xanth!jim
 ---------- Time flies like the wind, but fruit flies like bananas. ---------

ndd@duke.cs.duke.edu (Ned Danieley) (12/11/87)

In article <91@dlag1.UUCP> fm06222@dlag1.UUCP (Bill Wallace) writes:
>In article <205700003@prism>, billc@prism.UUCP writes:
>> 
>> 	    alias	where	"find \$cwd -name \!* -exec echo {} \;"
>> 
>> 	Question: Is there a better way to do this?
>
>On our copy of Unix (Gould UTX/32 - basically a BSD 4.3 clone) we have
>the option of establishing a database for find.  There's a script that
>we run out of crontab in the middle of the night to update the database
>daily, so the results are usually good enough.  The command is simple:
>
>	    find  <string>
...

Note that this is also available under SunOs 3.3, although it is not
documented (at least, not in my release of 3.3).

Ned Danieley (ndd@sunbar.mc.duke.edu)
Basic Arrhythmia Laboratory
Duke University Medical Center
Durham, NC  27710
(919) 684-6807 or 684-6942
~

gyounger@ARDEC.arpa (AED-EWD) (12/12/87)

  Unix message to your office,dated 9 December,from Harold Bamford
wanted to know about a better command to use to find a file in one's 
current directory. I recommend the following commands:
cat filename;page filename;pr filename!

jv@mhres.mh.nl (Johan Vromans) (12/13/87)

In article <205700003@prism> billc@prism.UUCP writes:
>
>	Right now, to find a file somewhere under my current directory,
>	I use the following alias:
>
>	    alias	where	"find \$cwd -name \!* -exec echo {} \;"
> .. etc ..

On our systems, a small cron script executes every night the following
command:

	find / -print > /dirfile

Finding a file somewhere can be done by grepping in the /dirfile.
Of course, the contents of /dirfile are not really up-to-date, but this is
just a minor drawback. "find" on the whole system (including mounted disks)
takes more than an hour, a grep in /dirfile much less than a minute.
-- 
Johan Vromans                              | jv@mh.nl via European backbone
Multihouse N.V., Gouda, the Netherlands    | uucp: ..{uunet!}mcvax!mh.nl!jv
"It is better to light a candle than to curse the darkness"

breck@aimt.UUCP (Robert Breckinridge Beatie) (12/14/87)

In article <1442@mipos3.intel.com>, pinkas@cadev4.intel.com (Israel Pinkas ~) writes:
> In article <205700003@prism> billc@prism.UUCP writes:
> >	I use the following alias:
> >	    alias	where	"find \$cwd -name \!* -exec echo {} \;"
> >	Question: Is there a better way to do this?
> 
> alias where 'find . -name \!!:1 -print'
> 
> There are a few problems with the alias:
> 
> 3) \!* will return all the arguments given to the alias.  If more than one
> is given, this will result in bad parameters to find.

Well, here's a small improvement with which I was inordinately pleased:

alias where	'set noglob ; find . `namepred \!*` -print ; unset noglob'

Here is the source for namepred:

	main(argc,argv)
	int argc;
	char **argv;
	{
	    if(--argc <= 0)
		exit(0);
	    printf("( ");
	    do  {
		printf("-name %s ",*++argv);
	    } while (--argc > 0 && (printf("-o ") || 1));
	    printf(")\n");
	}

it expands its argument list into:
	( -name arg1 -o -name arg2 -o ... -o -name argn )
which find can deal with.

The "set noglob" keeps csh from expanding metacharacters in the output of
namepred.  This allows things like:
	where '*file*'
to match files with, "file" as part of their name.

Like I say, I'm probably more pleased with this than I should be...  but what
the heck.
-- 
Breck Beatie
{uunet,pyramid!weitek}!aimt!breck
"Sloppy as hell Little Father.  You've embarassed me no end."

fletcher@b-mrda (12/17/87)

"du -a | grep filename" is my preferred method if I know it's somewhere
below my current directory.