Question on find inconsistency
Michael Hirsch
mdhirsch at gmail.com
Wed Nov 19 20:04:18 UTC 2008
On Tue, Nov 18, 2008 at 7:41 AM, Glenn R Williams <gloonie at earthlink.net> wrote:
> Hi all,
>
> I found a peculiar inconsistency with the "find" command and wonder if anyone
> can explain it (or even has it on their machine).
>
> It has to do with quoting the search pattern for the -name option. On my
> system, if I run:
>
> find / -name *.mp4
>
> It works fine, and finds the files. BUT, if I just change the extension:
>
> find / -name *.txt
>
> I get an error:
>
> find: paths must precede expression: file1.txt
> Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec]
> [path...] [expression]
>
> Does anyone know why this would be? I've used find for many years, but always
> quoted the -name argument.
If there is no file in the current directory that matches the filename
expression (*.mp4 or *.txt in your examples) the the filenames are not
expanded and the exact expression is passed to find by the shell. But
if there are one or more files that match, the expression is replaces
by the expansion and passed to find.
I would guess that there are no .mp4 files in the directory you are
running find in (not in /, but the current directory of your shell
which might be different). In that case find runs exactly as you
expect. It sounds like there are multiple .txt files in that same
directory and they are all passed to find which is expecting only one
name.
If there is exactly one file.txt in that directory, you won't get an
error, but you probably won't get what you expect, either. It will
look for files with exactly that name and no expression matching.
For safety's sake, it is usually best to quote or escape the wildcards like
find / -name \*.txt
That prevents globbing on the * and will do what you expect.
Michael
More information about the kubuntu-users
mailing list