Search code examples
bashglobls

List *.h and *.cpp using bash


I am using bash version 4.2.28 on Fedora 16. I have the extglob option set. I am trying to list all files matching *.h or *.cpp using ls *(h|cpp) but the command returns the following:

[agnel@damien cadcore]$ ls *(h|cpp)
ls: cannot access *(h|cpp): No such file or directory

I have verified that there are indeed several .h and .cpp files in my current directory. Am I doing something wrong or could this be a bug in bash or ls?

Update: Thank you for your answers. Using *.h *.cpp does what I need. However, I would still like to know why extglob didn't work like I expected.


Solution

  • The extended glob *(pattern-list) matches 0 or more occurrences of the following pattern list. It does not match an arbitrary string followed by something from the option list. You want:

    $ ls *.@(h|cpp)
    

    This matches something, followed by a period, followed by either "h" or "cpp"