Mailinglist Archive: opensuse (4547 mails)

< Previous Next >
Re: [SLE] Is there a "find file on system" command?
  • From: Nick LeRoy <nleroy@xxxxxxxxxxx>
  • Date: Mon, 17 May 2004 20:06:09 -0500
  • Message-id: <200405172006.09734.nleroy@xxxxxxxxxxx>
On Monday 17 May 2004 7:35 pm, James Knott wrote:
> Robert Paulsen wrote:
> > On Monday 17 May 2004 03:43, Jerome Lyles wrote:
> >>Password:
> >> # find / -name *.rpm
> >>find: paths must precede expression
> >>Usage: find [path...] [expression]
> >> #
> >>
> >>What happened? Isn't '/' enough path for this command?
> >>Thanks,
> >>Jerome
> >
> > You need to put quotes around the expression you are looking for if it
> > contains special characters that the shell will act on (in your case the
> > *). Try this...
> >
> > find / -name "*.rpm"
> >
> > You want the find command to see the *, not the shell.
> It works fine for me without the quotes. However, this is on Red Hat 7.3.

Sometimes it will, and sometimes it won't. To understand why, you need to
better understand how regexs are expanded under UNIX.

To use the above example, let's say I type this:

$ find / -name *.rpm

Now, if there are no in the current directory that match the '*.rpm' pattern,
the shell will try to expand this pattern, will find no matches, and thus
find will get invoked with argv:
argv[0] = "find", argv[1] = "/", argv[2] = "-name", argv[3] = "*.rpm".

Now, let's assume that in the current directory, there are two rpm files:
"file1.rpm" and "file2.rpm". Now, the *shell* will expand '*.rpm' to
"file1.rpm" and "file2.rpm", and, thus, will invoke find with argv:
argv[0] = "find", argv[1] = "/", argv[2] = "-name", argv[3] = "file1.rpm",
argv[4] = "file2.rpm".

Find will look at this, and say "huh?". It's only expecting a single argument
to '-name', but now you've passed it two.

It's even worse if there's only one match "file.rpm"; the shell will expand
"*.rpm" to "file.rpm":

argv[0] = "find", argv[1] = "/", argv[2] = "-name", argv[3] = "file.rpm".

Now, find will see a single argument to "-name", as it expects, but the
pattern being passed to it is "file.rpm" _not_ "*.rpm" as you expected. find
will silently do exactly what you told it to do, but not what you expected.

The moral of the story is that in UNIX, is the *shell* that does the expansion
of command line arguments.

Does this help?


<<< Why, oh, why, didn't I take the blue pill? >>>
/`-_ Nicholas R. LeRoy The Condor Project
{ }/
\ / nleroy@xxxxxxxxxxx The University of Wisconsin
|_*_| 608-265-5761 Department of Computer Sciences

< Previous Next >