Negation in Bash Globbing

$ ls -1
a.1
b.1
x_a.1
x_b.1

$ ls -1 [^a]*
b.1
x_a.1
x_b.1

$ ls -1 *[^a]*
a.1
b.1
x_a.1
x_b.1

The last result is not as expected.
Why?
Thanks.

Hello carloszhang,

While using * the preceding item will be matched zero or more times. Here you haven't mentioend any thing in last expressions so it is taking everything as a match. Also * to be considered as a greedy character so it will go for maximum search of the characters.

So either you do ls -1 *[^a]* or ls -l * I think it should give you same results.

Thanks,
R. Singh

1 Like

What RavinderSingh says is correct for regexes, but not for shell globbing. * itself is the wildcard character, matching any sequence of any chars.

So - *[^a]* will match anything followed by a non-"a" char followed by anything. that would exclude only filenames consisting of nothing but "a"s

---------- Post updated at 13:38 ---------- Previous update was at 10:52 ----------

As you are using bash, did you consider extended pattern matching? man bash :

3 Likes