Search code examples
linuxbashshellrhel

How to use -regex in the find command on Linux command line


The objective is to find and list anything with "messages" and/or "error.log" etc.. in the beginning then list both "messages.1..99" and "error.log.1..99" using regular expressions.

This command works for however, it would require me to make many -or searches, but to simplify, I would like to have multiple in a set within the search. Like for instance:

# find /var/log -maxdepth 1 -type f -size +1M -name [messages|error.log|secure.log|kern.log...]?[0-9]|[0-9][0-9] ! -iname "*.gz"

not

# find /var/log -maxdepth 1 -type f -size +1M -name "messages?[0-9]" -o -name "messages?[0-9][0-9]"

How might I perform this command with regular expressions?

# find /var/log -maxdepth 1 -type f -size +1M -name "[messages,error.log,kern,secure]?[0-9]" ! -iname "*.gz"

My attempt with regex doesn't print anything in standard out:

# find /var/log -maxdepth 1 -type f -size +1M -regex -name "[messages,error,kern,secure]?[0-9]" ! -iname "*.gz"

Solution

  • Try this:

    find /var/log -maxdepth 1 -type f -size +1M -type f -regextype egrep -regex '.*(messages|error|kern|secure)\.[0-9]+.*' -not -name \*gz