Kenneth Worden Kenneth Worden - 6 months ago 39
Linux Question

Unix find not respecting regex

I'm trying to do a simple

in my
directory to find all syslog files that are not zipped. What I have so far is the regex:


So this would find
, etc and skip over the gzipped logs like
or anything else not matching the pattern of the aforementioned syslogs. I'm doing a pretty basic find command, too:

find /var/log -regextype posix-extended -regex "syslog(\\.[0-9]*)?$"

However, I always get an empty result! Now, I thought the regex I wrote was POSIX-extended compatible, but it doesn't seem to be so. Here are variations of the command I ran, to no avail:

find /var/log -regextype posix-extended -regex "syslog(\\.[0-9]*)?$"

sudo find /var/log -regextype posix-extended -regex "syslog(\\.[0-9]*)?$"

find /var/log -regextype posix-extended -regex "syslog"

find /var/log -regextype posix-extended -regex "(syslog)"

This following works as expected by listing all files in the directory, however, so I know my command format is correct.

find /var/log -regextype posix-extended -regex ".*"

What am I doing wrong?


The regex pattern you provide needs to match the whole path. That means that you don't need to anchor it at the beginning and end with ^ and $, it's already implicitly anchored at both ends. But you do need to provide a leading .* or something similar if the rest of your pattern should match somewhere other than the beginning (and remember, find paths always include a directory, even if it's .).

find . -regextype posix-extended -regex '.*syslog(\.[0-9]*)?'

works for me.