Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

I am attempting to search through all the files in a directory for text matching the pattern of any arbitrary directory. The output of this I hope to use to make a list of all directories referenced in the files (this part I think I can figure out on my own).

I have looked at various regex resources and made my own expression that seems to work in the browser based tool but not with grep in the command line.


My understanding so far is the above expression will look for the beginning / of a directory then look for an indeterminate number of characters before looking for a repeating block of the same thing.

Any guidance would be greatly appreciated.

share|improve this question
Your parentheses and "+" are inside your character class: "[" and "]" define a class of characters you are searching for; including "(", ")", and "+" in between the "[ ]" means you want to match these characters among others. I think you probably want the parentheses and the "+" outside of your character class: ([/\w]+). That should match and capture any combination of "/" and alphaneumeric characters. I didn't post this an answer because I get lost trying to use regex with grep all the time. – erewok Jul 1 '13 at 20:09
up vote 1 down vote accepted

If I understand you correctly, you want a regex that will allow you to use grep to identify paths. I am not sure what you are trying to do with the regex you have posted. Why do you want something to be repeated? Paths only need a single slash: /etc.

Anyway, if you want any kind of path (paths can also contain non-word characters like {,[,( spaces and new lines etc), try this:

grep -E '/[^/]+' *txt
           |  |--------> one or more
           |-----------> A character class, '^' in a character class means NOT,
                         so this class means "anything that is not /".

The -E tells grep that the pattern you will give it shoujld be interpreted as an Extended Regular Expression rather than it's default, Basic Regular Expression. EREs support + for "one or more" which is needed to find only strings with at least one non-slash character after the first slash.

If you want to find only paths with more than a single /, you can do something like

grep -E '/[^/]+/[^/]+' *txt  

Paths can also end with a slash, if for some reason you want to keep such trailing slashes use (/? means "find zero or one /):

grep -E '/[^/]+/[^/]+/?' *txt  

More specifically, the regex you are using fails for various reasons. First of all, as @erewok pointed out, you are using parentheses and a + inside brackets. Since brackets specify a character class, anything within them is treated as one of the characters to be found (with the exception of ^ which makes it a negated character class).

So, [(/\w+)]+ means find any of (,/, any word character (\w),+ or ) one or more times. In any case, \w is not recognized by grep unless you use Perl Compatible Regular Expressions. You can activate these in grep with the -P flag. For example, this will match a path like /etc:

grep -P '/\w+' *txt

If you know that your paths will always consist of word characters (ie a-z,A-Z,0-9, and _), you can use an expression like the above but since one can never be sure, using something less strict, lke my fist suggestion is preferable.

share|improve this answer
Thank you for this explanation. – BrandonKowalski Jul 2 '13 at 12:05

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.