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.

  1. What is ^?
  2. What does mean the parenthesis () in this context
  3. The command ^(*.c||*.md) also works… Should I use | or ||?
Asked By: Rubem Pacelli

  1. In Zsh, ^ is a glob operator available when EXTENDED_GLOB is set, matching anything except the following pattern.

  2. 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.

  3. ^(*.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 printed raw on 1 Column. N glob qualifier for NULL_GLOB to avoid an error if no matching file is found (and get no output from print).

Answered By: Stephen Kitt
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.