ls pattern matching
9,989
Solution 1
I think you are looking for the brace expansion {asd,qwe}
:
$ ls foo.{asd,qwe}
foo.asd foo.qwe
Solution 2
The globbing pattern would be ls foo.@(asd|qwe)
. This works
- out of the box in ksh;
- in bash also if "extended globbing" is activated with
shopt -s extglob
; - in zsh if ksh-style globs are activated with
setopt ksh_glob
.
Related videos on Youtube
![Gauthier](https://i.stack.imgur.com/nABuD.jpg?s=256&g=1)
Author by
Gauthier
Updated on September 18, 2022Comments
-
Gauthier almost 2 years
I know that
[]
is working inls
pattern matching:$ ls foo.c foo.h $ ls foo.[ch] foo.c foo.h
but I cannot find where this is documented.
I would like to know the syntax that would match these:
$ ls foo.asd foo.qwe
This is the best guess I had:
ls foo.[{asd}{qwe}]
. It did not work.-
Janis about 9 yearsIt is not "pattern matching" from
ls
but "shell globbing"; see the shell documentation for all the details.ls
gets just to see what the shell expands.
-
-
Gauthier about 9 yearsThis is what I was looking for. @Janis comment helped me find documentation, eg here: linux.die.net/Linux-CLI/x11655.htm
-
Janis about 9 yearsNote that this is not pattern matching, but brace expansion; even if it works for this specific case you cannot match arbitrary patterns with it.
-
heemayl about 9 years@Janis: I think OP is looking for this actually, the examples on the question also suggests so..probably he/she could not clarified query enough..see OP's comment above..
-
Janis about 9 years@heemayl; as said, it seems to suffice for his needs. But in his question he clearly spoke about "pattern matching" and "the syntax that would match".
-
Gauthier about 9 yearsAccording to this: linux.die.net/Linux-CLI/x11655.htm , this brace expansion qualifies as standard wildcards or globbing pattern. I am not sure how "pattern matching" differs from "globbing pattern". Strangely,
man 7 glob
describes[]
but not{}
. -
Peter.O about 9 yearslink to Bash Extended Globbing
-
heemayl about 9 years@Gauthier: globbing is synonymous to pattern matching in shell, i think what Janis meant is to use extented pattern matching using extended glob..
-
Janis about 9 years@heemayl; yes, this was the semantics. @Gauhier; no, brace expansion is not equivalent to or from the cathegory "pattern matching". Brance expansion is just a textual expansion and will produce an error if used in a globbing context and one of the generated file names is not existing. Observe the difference of
ls *.{foo,bar,baz}
and*.@(foo|bar|baz)
in case there's no*.baz
file. Similarly the globbing pattern*.[ch]
is not equivalent to the brace expansion*.{c,h}
. - That is the main reason why I would not (or only in very specific cases) suggest to use brace expansion for globbing.