Search code examples
bashpathpattern-matchingglob

Including path portions in xtended pattern matching operators


Given the following directory structure, where all leafs are files,

a
├── b
│   ├── d
│   │   └── f
│   └── e
│       └── g
└── c
    ├── d
    │   └── h
    └── e
        └── i

the following command lists the files f and h

$ ls a/@(b|c)/d/.

but the following fails in listing f and i

$ ls a/@(b/d|c/e)
ls: cannot access 'a/@(b/d|c/e)': No such file or directory

Clearly, I cannot put slashes in the pattern-list. My question is one, but maybe breaking it into pieces is better:

  • Why?
  • Is there any intrisic reason why doing this is impossible?
  • How can I obtain the same effect with a oneliner?

Solution

  • My gut is telling me that Filename Expansion can generate filenames but not pathnames.

    This is a case where you can use Brace Expansion though:

    ls a/{b/d,c/e}