Re: [SLUG] dot's function in *nix

From: Paul M Foster (paulf@quillandmouse.com)
Date: Mon Dec 31 2001 - 01:48:23 EST


On Sun, Dec 30, 2001 at 03:39:59PM -0500, Mario Lombardo wrote:

> In the thread subject 'A "whatis" catalog', I was once again
> mystified by the use of the dot in Unix-like systems. I originally
> thought the use of dot was merely to state "here" or "from here" as
> in the example of filesytems use:
>
> ls .
> sh ./myscript
>
> But the dot turned out to be more than that when I saw people using
> it in other ways; most recently from Ed Centanni's REply to my
> question with this as the best working model:
>
> apropos . | sort > mycatalog.txt
> versus...
> apropos * | sort > mycatalog.txt
>
> I don't understand the literal interpretation of the dot in this
> example versus using an asterisk. Actually, an asterisk doesn't
> discover as many commands as the dot. In fact, doing a search for
> the command chfn doesn't come up in the asterisk solution, yet it
> comes up in the dot solution. This seems opposite of what would work
> in the DOS world--which is my background.
>
> URL reading sources are fine. I just want to understand how to
> utilize this little gem in other ways...if there are any more.
>
> Thanks,
> Mario

The dot in front of a filename also signifies that the file is "hidden"
from standard file-type commands like ls, cp and such. In the case
above, this wouldn't apply, as the dot is _not_ part of the filename:
./myscript (the filename is myscript in the ./ [this] directory; if it
were hidden, it would be ./.myscript). In that case, it's exactly what
you said-- "here" or "this directory".

To make a long story short, "apropos ." gives you everything in the
whatis file, because it treats the dot as a regular expression for "any
character". And the star [inadvertantly] gets expanded by the shell to
be all the files in your current directory, so you get the output of
apropos against _each_ filename in your current directory. For a more
complete explanation, read on.

The man pages don't shed any light on the use of the apropos command
with the dot or the star. However, apropos is a shell script, so you can
look at the source to determine what it's doing. Apropos runs against
the whatis files, wherever they are. In the case of Red Hat, that's
/var/cache/man/whatis, $manpath/whatis ($manpath will generally be
various directories, like /usr/share/man), and /usr/lib/whatis. The
whatis files are compiled by the makewhatis program, which often runs
as a cron job periodically to rebuild the whatis files. Whatis files are
basically the output of man against each command there's a man page for.
You can run "less /var/cache/man/whatis" and you'll see what I mean.

Apropos basically runs a grep command (twice) against the whatis files
that it finds, looking for whatever you've given it. If you give it a
'z' (as in "apropos z") it will find every line in the whatis files that
contains a 'z' and output them.

>From some experimenting with star and dot, here is what I think is
happening. With the dot (.), apropos (and grep) look for _anything_ in
the whatis files. That is, it uses the dot as part of a regular
expression. In that case, the dot would represent any character, so
you'd get the whole contents of the whatis files. In the case of the
star (*), I believe that before executing grep, the shell expands the
star to include the filenames of all the files in the current directory.
So what you get is something like:

grep -ia thisfile thatfile theotherfile ... /var/cache/man/whatis

assuming thisfile, thatfile, etc. are all files in your current
directory. If your home directory is populated with a few files, you can
test this by running

apropos * > junk

and then look at the junk file. You'll see that apropos attempted to run
the grep against every file in your home directory. So, for example, if
you have a "Mail" subdirectory in your home directory, you'll get the
result of running "apropos mail" (apropos is case insensitive) along
with similar output for each and every file in your home directory.

(I don't really think apropos was intended to be run with the star. The
behavior of the shell expanding the star prior to executing grep is
inconsistent with the usual behavior of apropos. This may be material
for a bug report.)

All of this is based on experiments with the apropos command on a Red
Hat 7.2 system (apropos from man 1.5i1), run from my home directory. If
your apropos command is not the same as mine, YMMV.

HTH,

Paul



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:28:19 EDT