1

This output is rather self-explanatory:

XXXXX@debianvirtualbox:~$ locate -r "^/var/lib/tomcat[0-9]/.*" -l 10
/var/lib/tomcat8/conf
/var/lib/tomcat8/lib
/var/lib/tomcat8/logs
/var/lib/tomcat8/webapps
/var/lib/tomcat8/work
/var/lib/tomcat8/webapps/ROOT
/var/lib/tomcat8/webapps/websight
/var/lib/tomcat8/webapps/ROOT/META-INF
/var/lib/tomcat8/webapps/ROOT/index.html
/var/lib/tomcat8/webapps/ROOT/META-INF/context.xml
jakub@maredadebianvirtualbox:~$ locate -r "^/var/lib/tomcat[0-9]{1,2}/.*" -l 10
XXXXX@debianvirtualbox:~$

I am trying to list first ten (-l 10) entries matching tomcat installation directory. If I just use [0-9] it properly matches tomcat8, however if I add a quantifier [0-9]{1,2} it matches nothing. Same goes for quantifiers + and ?, however * seems to work fine and so does this expression:

^/var/lib/tomcat[0-9][0-9]*/.*

Why this happens and what's a good workaround?

1 Answer 1

3

Try --regex (without a 'p') rather than -r (aka --regexp). This tells locate to use extended regexps rather than basic.

locate --regex "^/var/lib/tomcat[0-9]{1,2}/" -l 10 

alternatively, escape { and } with \ to make them special in basic regex.

3
  • BTW, the above applies to mlocate's version of locate. I can't remember if it also applies to GNU locate from findutils, I haven't used anything other than mlocate for years.
    – cas
    Oct 2, 2015 at 7:58
  • Yeah, it works even with normal locate, thanks. Oct 2, 2015 at 8:06
  • GNU locate has a --regextype option to specify the type of regular expression. gnu.org/software/findutils/manual/html_mono/…
    – cas
    Oct 2, 2015 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.