Search code examples
regexbashpattern-matchingglob

Escaping in a Bash extended pattern @(..)


I stumbled across "Extended pattern" extglob for Bash. http://wiki.bash-hackers.org/syntax/pattern

I'm investigating this case:

Imagine a directory structure like this:

/basedir/2017/02/26/uniquedirs1/files.log 
/basedir/2017/02/27/uniquedirs2/files.log 
/basedir/2017/02/28/uniquedirs3/files.log 
/basedir/2017/03/01/uniquedirs4/files.log
/basedir/2017/03/02/uniquedirs5/files.log

If I'd like to list the files of the directory 2017/02/27 and 2017/02/28 I can simply write:

ls /basedir/2017/02/@("27"|"28")/*/files.log

Awesome! \o/

Now the question. How can I define multiple directories in a Bash extended pattern?
This doesn't work:

ls /basedir/2017/@("02/28"|"03/01")/*/files.log
ls /basedir/2017/@("02\/28"|"03\/01")/*/files.log

Is there any way I can get to define multiple directories for "Extended pattern"?


Solution

  • Pathname generation only applies patterns to pathname components; they never match the / that separates components. Brace expansion would be cleaner here:

    ls /basedir/2017/{2/28,3/01}/*/files.log
    

    which would first expand to

    ls /basedir/2017/2/28/*/files.log /basedir/2017/3/01/*/files.log
    

    before performing pathname generation on the two resulting patterns.