0

Say I have a list of files in dir like so:

blackneasy-sq.png
dima-sq.png
envoy-sq.png
fox-sq.png
freeze-sq.png
ministarstvo kulture-sq.png
naxi-sq.png
pick-sq.png
pink-sq.png
wink-sq.png
brick-sq.png
rider-sq.png
sixt-rent-a-car.png
slavija hotel-sq.png
temet-sq.png
tepe-sq.png

How would I specify exclude pattern to show all files except those that contain ck chars glued together anywhere in filename?

So in a resulting list, files:

blackneasy-sq.png
pick-sq.png
brick-sq.png

should not be shown.

I tried this without success.

print -l [[:alpha:]]*^ck*.*

1 Answer 1

2

Use the ~ operator: THIS~THAT matches files that match the pattern THIS but not THAT. This requires the extended_glob option (which is automatically turned on in some contexts such as completion function, but is not the default state).

print -l -- *.png~*ck*

You can abbreviate *~THAT to ^THAT (also requiring extended_glob), so if you don't want to limit to .png files, you can use

print -l -- ^*ck*
6
  • Is there a way for [[:alpha:]] not to be eager with * or # or does it always consume as much as possible!?
    – branquito
    Jun 3, 2016 at 20:54
  • 1
    @branquito * and # are always eager. But eagerness only matters when you have capture group, not with regex matching alone. Jun 3, 2016 at 20:57
  • Also being curious, if it weren't for THIS~THAT, would it be possible to do this some other way without help of grep or whatever? Thanks
    – branquito
    Jun 3, 2016 at 20:57
  • 1
    @branquito grep would be hard to use for that unless you know that your file names can't contain any special characters. The plain sh way of doing this is for x in *.png; do case "$x" in *ck*) continue;; esac; {process $x}; done Jun 3, 2016 at 20:59
  • 1
    @branquito [[:alpha:]]*^ck matches anything that starts with a letter and ends with something that isn't ck. Since ck can be matched by *, [[:alpha:]]*^ck matches anything that starts with a letter. Eagerness wouldn't help: they still allow the star to match as long as it wants, eagerness is just a matter of preference when there are several possible ways to match. Jun 3, 2016 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.