Alexandru Olaru
Alexandru Olaru

Reputation: 7092

How to translate a pcre (php) regex pattern in bash?

I have the following regex pattern that is working nicely in php

/(^((?!thumb).)*\.jpg)/

My task is to translate this regex in bash, the ideea is to find all the .jpg files from a directory (recursively) that do not contains the thumb word in the file path.

I use a structure like this:

find /folder/path -regex '(^((?!thumb).)*\.jpg)'

But i have zero results ? Is there something wrong with my regex in bash?

Upvotes: 1

Views: 572

Answers (3)

Benjamin D
Benjamin D

Reputation: 286

find . -iname '*.jpg' -not -iname "*thumb*"

Upvotes: 1

123
123

Reputation: 11216

Think the best you are going to get with find is using two different criteria instead of a single regex

find . -name '*.jpg' ! -regex '.*thumb.*'

Upvotes: 1

Thomas Ayoub
Thomas Ayoub

Reputation: 29431

You can't use negative lookahead with find since it's a POSIX regex. You may use this workaround:

find . -iname '*.jpg' | grep -v "thumb"

Which will list all jpg files then, thanks to grep exclude all files containing thumb

Upvotes: 1

Related Questions