locate is very versatile can take -r and a regexp pattern, so you can do lots of sophisticated matching. For example, to match directories
a1 and so on use
'/a[0-9]*/'. This will only show directories with files in them since you need the second
/ in the path. To match the directory alone use
$ to anchor the pattern to the end of the path,
Note, there are at least 2 versions of the
locate command, one from GNU, and one from Redhat (known as mlocate). Use
--version to find which you have. They differ slightly in the regex style. For example, if we change the above pattern
'/a[0-9]*$' to use
+ instead of
* to avoid matching
a on its own, then mlocate needs
\+ and gnu just
For example, to match a directory
a and all underneath it you might use for both versions
locate -r '/a\(/\|$\)'
For mlocate you might prefex
--regex which uses extended syntax
locate --regex '/a(/|$)'
To do the same for gnu locate you would need to add option
--regextype egrep, for example.