bug-gnu-utils
[Top][All Lists]
Advanced

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

Re: grep


From: Bob Proulx
Subject: Re: grep
Date: Sun, 14 Jun 2009 18:43:02 -0600
User-agent: Mutt/1.5.18 (2008-05-17)

Chris Jones wrote:
> On Sun, Jun 14, 2009 at 02:09:12PM EDT, lamarr wrote:
> > $ ls -a | grep ^\\.
> > does the job.
> 
> Not here - vanilla debian "lenny" install.
> $ ls -a | grep ^\\.* 

However ^\\.* isn't the same as ^\\. for two reasons.  One is that '*'
is a shell metacharacter and needs to be escaped to protect it from
shell file glob expansion.  It is unlikely in this case that the
pattern would match an actual file name but possible and therefore the
glob characters should be quoted.  In other words, what you see isn't
what you get.

Secondly the '*' in a regular expression means zero or more
occurrences of the previous expression.  In this case a literal '.'.
This completely negates the requirement of having a dot in the
pattern.  Obviously '\.*' isn't the same as '\.' since the \. then
becomes optional.

Let me guess that you really want something more like one of these:

  ls -A | grep '^\.'

Or just use ls directly.

  ls -d .??*      # Almost.  Skips two letter files like ".a" but is
                  # one of the classic way of doing this type of thing.
  ls -d .[!.]*    # Almost.  Skipps two dot names like "..a".

The "almost" versions are usually "good enough" for me from the
command line that I often use them even though I know they are not
perfect.

> does .. and interestingly this does not recursively list the contents of
> the "dot directories".
> 
> Different than:
> 
> $ ls -a .*

As a good guideline if you ever want to list files using ls but using
the shell to do the file glob expansion first into a list of files
then you should always include the -d option to prevent the resulting
directory from being listed itself.  You really do want a -d there.
Also you don't need the -a there since ls isn't listing the directory.
The others don't list .. because .. isn't appearing one the command
line as an argument.  Having program parameters to ls versus not
having program parameters are really quite different cases.

Actually even though 'ls' is convenient it isn't needed.  You could
always use echo to print out the shell file glob expansion.  Try this
for example.

  for i in .*; do echo $i; done

That is completely internal to the shell without any external commands
at all.  The ls command isn't really needed there.  But I like it with
the -F and -C options.

  ls -adFC .??*

Since I 'alias ls="ls -F" and -C is the default when the output is a
tty device and -a doesn't matter when giving ls paramets it means all
I really ever type in is:

  ls -d .??*

I am sure there are more correct ways of saying .??* in a nice
standard non-shell specific way but I have forgotten them.  I am sure
someone will jump in and correct me.

Bob




reply via email to

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