[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Odd "ls" behavior in version 4.0p

From: Bob Proulx
Subject: Re: Odd "ls" behavior in version 4.0p
Date: Sat, 20 Oct 2001 10:03:53 -0600

> I was searching for the file ".foo" in my filesystem and found the
> following curious behavior:
> ls -a *foo*  lists nothing (a surprise to me!)

But I was not surprised.  :-)

> ls -a .*foo*  lists .foo, as expected.
> I would think that the -a option should cause ls to list the hidden
> file, even though I'm not actually specifying the dot. I noticed that ls
> on HP-UX has the same behavior, but it certainly seems non-intuitive.

You are getting confused thinking that the ls command is seeing the
file wildcards.  It is not.  The shell expands wildcards like '*'.
Commands like 'ls' see the result only after expansion.

The shell interpreter is expanding the command line glob characters
prior to handing the arguments off to your command.  This is a simple
form of regular expression matching designed to make file name
matching easier.  This provides a consistent interface to all programs
since the expansion code is common to all programs by being in the
interpreting shell instead of in the program itself.  Commands in UNIX
do not see the '*foo*' or any of the shell metacharacters, unless
there is no match, more on that later.  Commands see the expanded out
names which the shell found matched file names in current directory.

The '*' is the "glob" character because it matches a glob of
characters.  But it only matches files in the current directory.  It
does not go out and list files in other directories.  The shell
matches and expands glob characters and hands of the resulting
information to the command.

You can double check this by using the echo command.  This is built
into most command shells, for example into bash.  Try echo *foo*.  Try
echo .*foo*.  In your example the first would print out *foo* if
nothing matched but would print out all file names that did match.
The command would see the result and has no idea that you provided a
wild card to match against file names.

Note in particular that '*' does not match a leading dot.  This is by
design, 30 years of use, standards, and is not likely to change.
Therefore *foo* will never match something that matches .*foo*.

For something completely different but more in the philosophy of unix
you might try the following.

  ls -a | grep foo

Bob Proulx

reply via email to

[Prev in Thread] Current Thread [Next in Thread]