[Top][All Lists]

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

Re: [Help-bash] Glob star pattern does not match files beginning with a

From: Eric Blake
Subject: Re: [Help-bash] Glob star pattern does not match files beginning with a period
Date: Wed, 15 Jul 2015 14:19:49 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.0.1

On 07/15/2015 01:58 PM, Michael Convey wrote:

[top-posting on technical lists makes your responses harder to read]

> ​I've done some more testing of this and found no difference between
> ​ls -dl .[!.]?*
> and
> ls -dl .[!.]*

Then you obviously didn't try it on a file named '.a'.

> In both cases the . directory does not appear in the output. Why doesn't it
> appear in the second command? Does [!.] require a second character?

Yes, [!.] requires a second character.  The trailing ?* then requires a
third character.  So you have a difference of whether '.a' vs. '..a' vs.
'...' get accepted, depending on whether you forbid a second dot vs.
whether you require at least three characters, coupled with a desire to
not list files twice through two different globs.

>>> Almost. You want .[!.]* and ..?* so you don't miss files like ..foobar
>> Good point. So, really the full command should be:
>> ls -d * .[!.]?*
>> ​ ..?*

No. That misses files like '.a'.

Both of these three-glob approaches can be used to obtain all file names
that are not '.' or '..':

ls -d * .[!.]* ..?*


ls -d * .??* .[!.]

but you still have to deal with the fact that unless nullglob  is
enabled to eliminate a glob that has no matches, you are then passing
unexpanded globs to ls that may result in listing a valid file name
twice, or in ls reporting an error about a file not found.

There is no two-glob pattern that can cover all file names except for .
and .., unless you resort to bash's extended globs (at which point, you
might as well do it in a single glob that uses alternation).

Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

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