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.

In my centOS there is a lot of log files in my folder /log

/log/log.20120308
/log/log.20120308.1
/log/log.20120308.2
/log/log.20120308.3
/log/log.20120308.error
/log/log.20120308.error.1

Apparently if I use command like below

find /log -type f -iregex '.*/'log.20120308'.[0-9]*' -print

I will only get

/log/log.20120308.1
/log/log.20120308.2
/log/log.20120308.3

I hoping to have the right regular expression to get the list below but fail

/log/log.20120308
/log/log.20120308.1
/log/log.20120308.2
/log/log.20120308.3

I couldn't figure out the regular expression to select extension .1, .2, .3 or blank

share|improve this question
    
Thanks guys :) Love you all –  forestclown Mar 10 '12 at 6:59

2 Answers 2

up vote 3 down vote accepted
find /log -type f -iregex '.*/'log.20120308'\(\.[0-9]*\)?' -print

the ? indicates one or none, and you should escape the dot (before the [0-9] since dot matches everything), so if you don't escape the dot, you would also get /log/log.20120308_1 in the result if that file is existed.

share|improve this answer
    
I like this version also. And note that you can leave out that last * if you only have single-digit numbers after the . that you want to find. –  Lars Rohrbach Mar 10 '12 at 2:59

One version:

find /log -type f -iregex '.*/'log.20120308'[.0-9]*' -print

Of course, this would also select a file that was named /log/log.20120308.., but I don't imagine that would be a likely problem.

Another way would be to use -o as an "or" operator, with escaped parentheses around the operands:

find /log -type f \( -iregex '.*/'log.20120308'.[0-9]*' -o -iregex '.*/'log.20120308 \) -print

Oh, one more update for the use of -iregex: you can simply your quoting somewhat, since '.*/log.20120308.[0-9]*' would select the same as '.*/'log.20120308'.[0-9]*' Putting this together with the suggestion by @md-gao, you could get this version:

find /log -type f -iregex '.*/log.20120308\(\.[0-9]\)?' -print
share|improve this answer

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.