bash--wildcard of [characters]

Is there a way to use [characters], such as [:upper:], as if they were a wildcard? for example, lets say I have a directory with the following files:

AAA
AA
aaa
aa
BBB
BB
bbb
bb

now lets say I want to ls all the files that start with AT LEAST two lowercase letters. Instead of ls [[:lower]][[:lower:]]* is there a more efficient way of doing this, which would be essentially be "wildcard number of [[:lower:]]'s and then a wildcard?"

Also, what about not just starting with two lowercase letters, but starting with ONLY 2 lowercase letters, not * lowercase letters. (i.e. ? instead of *)? thanks

1

1 Answer

You could use extended globbing:

If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized. In the following description, a pattern-list is a list of one or more patterns separated by a ‘|’. Composite patterns may be formed using one or more of the following sub-patterns:

?(pattern-list)
Matches zero or one occurrence of the given patterns.

*(pattern-list)
Matches zero or more occurrences of the given patterns.

+(pattern-list)
Matches one or more occurrences of the given patterns.

@(pattern-list)
Matches one of the given patterns.

!(pattern-list)
Matches anything except one of the given patterns.

As for what you asked, I can't make sense of this one:

... all the files that start with AT LEAST two lowercase letters ... "wildcard number of [[:lower:]]'s and then a wildcard?"

A wildcard matches zero or more, which is not the same as two-or-more. Any number of [[:lower:]]s can be matched with the extended glob *([[:lower:]]). "At least two" doesn't need extended globbing at all: [[:lower:]][[:lower:]]*.

... starting with ONLY 2 lowercase letters, not * lowercase letters

[[:lower:]][[:lower:]]?([^[:lower:]]*)

Which is two lowercase characters optionally followed by (anything other than a lowercase character and anything).

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like