What does mean `^(*.c|*.md)`
I am sorry if it is a potential obvious question, but I have no idea what ^()
means in
ls ^(*.c|*.md)
On zsh
, this argument expands to everything but *.c
and *.md
files.
- What is
^
? - What does mean the parenthesis
()
in this context - The command
^(*.c||*.md)
also works… Should I use|
or||
?
-
In Zsh,
^
is a glob operator available whenEXTENDED_GLOB
is set, matching anything except the following pattern. -
The parentheses group a pattern; this is useful in particular with disjunctions (
|
) where parentheses are required so that the disjunction isn’t interpreted as a pipe. -
^(*.c||*.md)
means “anything except files matching*.c
, or files with an empty name, or files matching*.md
”; the empty name is useless, so you should use|
. You could also write it^*.(c|md)
to avoid repeating the*.
.
You should also use the --
option delimiter for ls
as otherwise if any of the file names started with -
, they’d be treated as options by ls
.
If you don’t intend ls
to list the contents of those files resulting from that glob expansion that happen to be of type directory, you should use the -d
option. Or just use the print
builtin to print those file names; ls
would only be useful with options like -l
to get metadata information from those files.
ls -ld -- ^*.(md|c)
print -rC1 -- ^*.(md|c)(N)
Here with the file names print
ed r
aw on 1
C
olumn. N
glob qualifier for NULL_GLOB
to avoid an error if no matching file is found (and get no output from print
).