Search code examples
regexbashglob

Can a Bash extended globbing pattern be used to match a path NOT containing specific characters


In a bash script I would like to find files whose path does not contain a subdirectory with a "double extension". For example, if I am looking in subdirectories of /my/home for path/to/my/file and the following existed, the script would locate the first one only:

/my/home/subdirectory.1/path/to/my/file
/my/home/subdirectory.1.2/path/to/my/file

This is a simplified example: the 1 and 2 may be any string; the key point is they are like file extensions (i.e. separated by periods).

I can use ls

$ ls /my/home/subdirectory.*/path/to/my/file

but this returns both - I'd like to replace .* with .[^.]* but that's a regex and globbing doesn't use regex. I've tried extended globbing but I can't get it to work (I'm happy to admit I am unfamiliar with extended globbing):

$ shopt -s extglob
$ ls /my/home/subdirectory.!(.*)/path/to/my/file

I know I can use find to do this like so:

$ find /my/home/subdirectory* -regex "/my/home/subdirectory.[^.]*/path/to/my/file"

but I would expect a glob to be quicker. However, having read up on this, I think this is beyond the abilities of Bash's globbing, extended or otherwise. But I thought it a good question that could be worth discussing.


Solution

  • I think what you want is

    ls /my/home/direcectory.+([!.])/path/to/my/file
    

    which will match one or more non-. characters in the extension.