regex help with 'find'

How to do alternation using regular expressions in the 'find' command? Like say you want to find all files that do not match the names specifically "this" or "that" within a directory using regular expressions?

find . -type f \( ! -name '*this*' -a ! -name '*that*' \)

Gotta be a better way than typing -name a bunch of times?

If you have Linux, the -regex option is available, but not with standard find, no.

You could also just pipe its output through egrep which should be able to handle it fine.

1 Like

Awesome. I'm curious, could you get the same deal working for the files your searching in instead of the files your searching for?

Sorry, what?

like find "regex" -name "somefile"

where regex could be a bunch of directories that fall within the regex?

Yes, the regex matches the whole path.

But if you know where you want find to search, why not just give find those directories in the first place? Much less work for find to do.

I have had times when I would have liked to know how to do both. That's why I asked, I don't really need it now but I'm sure I will later.

How about a nested find command...

find start_dir -name "dirname_regexp" -type d -exec find {} -name "somefile" \;
1 Like

Yea that looks interesting, but looks like it could take a while to run. Probably want the pathname of start_dir to be as long as possible.