Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

config_foo_foo.txt
config_bar_bar.txt

but NOT

config_foo_bar.txt

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
add comment

3 Answers

up vote 1 down vote accepted

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

1.

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

Output:

config_foo_foo.txt
config_bar_bar.txt

2.

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

Output:

./config_foo_foo.txt
./config_bar_bar.txt
share|improve this answer
1  
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
show 5 more comments

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

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

See

share|improve this answer
1  
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
add comment

I believe you may need to do something like

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

Your Answer

 
discard

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.