1

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

3
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.

  • 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
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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