Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


ls -l /var/log | awk '{print $9}' | grep "^[a-z]*\.log."

outputs this:


but this:

ls -l /var/log | awk '{print $9}' | grep "^[a-z]+\.log."

outputs nothing.

Why? I just changed * to +. Isn't it similar? Operator + just needs at least one match, and * zero or more.

share|improve this question
up vote 18 down vote accepted

This is because grep (without any arguments) only works with standard regular expressions. + is part of the extended regular expressions, so to use that, you need to use grep -E or egrep:

ls -l /var/log | awk '{print $9}' | grep -E "^[a-z]+\.log."

Also, you can just do this if you don't want to use extended regular expressions:

ls -l /var/log | awk '{print $9}' | grep "^[a-z][a-z]*\.log."
share|improve this answer
Thanks. I now about that workaround, but I was wondering why "+" does not work. Now I know. Thanks again. – Marko May 9 '13 at 17:57

To elaborate on MiJyns answer, "special characters" like + do work in standard regex as well, but you need to escape them with a backslash. You might say, the default expectations are reversed between standard and extended regex:

In standard regex, characters match literally by default. For example, in grep "ab+" the + is a literal +. The regex would for example find "ab+ab", but not "abbbb". To use the "special meaning" of +, you need to escape it. So grep "ab\+" would find "abbb", but not "ab+ab" any longer. Because in the last example, the + is interpreted as the quantifier "one or many of that", in that case "one or many b".

In extended regex it's exactly the other way around. Here, you need to escape "special characters" to be treated literally. So grep -E "ab+" finds "abbb", but not "ab+ab". If you escape the +, it get's matched literally. So grep -E "ab\+" finds "ab+ab", but not "abbb".

share|improve this answer
What a legacy mess... ;-) like magic and supermagic re in vim. Urgh. The price to pay for backward compatibility... – Rmano Nov 9 '15 at 10:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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