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 ||?

1 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .