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

Can bash list with a repeated string? Lets say I wanted to match the files


but NOT


something like

ls -l config_*_*.txt

matches all three. Would I have to use something like grep and a regular expression to get the behavior

share|improve this question
up vote 1 down vote accepted

Scott's answer looks right. With find it looks like this:


find * -maxdepth 0 -regex "^config_\(.*\)_\1\.txt$"




find . -mindepth 1 -maxdepth 1 -regex "^\./config_\(.*\)_\1\.txt$"


share|improve this answer
Wouldn’t find . -mindepth 1 -maxdepth 1 be better than find * -maxdepth 0? – Scott Oct 10 '12 at 21:17
Ah yes thanks. I figured regex would be the solution since I couldn't find anything for bash to do it. Find is better than getting all of them then filtering with grep like I was thinking. – f4hy Oct 10 '12 at 22:47
@Scott fixed... – Radoo Oct 11 '12 at 14:51
@Scott find . would give you "./" at the beginning of each line... – Radoo Oct 11 '12 at 14:57
@Radoo: “find . would give you ./ at the beginning of each line.” OK, true. So what? – Scott Oct 11 '12 at 18:16

Yes, try the following code (using extented globs ) :

shopt -s extglob
ls config_!(*foo_bar*).txt


share|improve this answer
Oh that is fine for my example but wont match generic wild cards. It will just exclude anything with the string "foo_bar" but wont exclude bar_foo or baz_bar etc. I want any possible repeated string to be matched. I don't see how to do that with extglob but that is what I am asking. – f4hy Oct 10 '12 at 22:41
I was going to suggest config+(_*).txt, but that matches one or more of any _*, not one or more of the first thing that matches _*. – chepner Oct 12 '12 at 18:55

I believe you may need to do something like

command $(ls -d config_*_*.txt | grep '^config_\(.*\)_\1\.txt$')
share|improve this answer

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.