user1231737
user1231737

Reputation: 33

regex to exclude few files but include rest

I have multilevel directory structure and I want to list all (png,jpeg files), except files, say, sam.png, pam.jpeg etc from any directory

Ultimately, I want to list all files as

a/b/c/d*.jpg (inlude all jpg, but I want to exclude sam.png from a/b/c/d directory)

a/b/c/d*.jpeg

a/b/c/d*.png

a/e/f/k/h/*.png

...and so on

How do I do that?

Upvotes: 0

Views: 887

Answers (2)

Vikram Bhosale
Vikram Bhosale

Reputation: 48

Use following regex -

.(jpg|jpeg|gif|png)$(?<!sam.png|pam.jpeg)

This uses negative look behind.

Upvotes: 3

Regedor
Regedor

Reputation: 144

I'm not completly sure of the context of you problem.

However you can use find . to list and files under a directory (recursive).

Use egrep to filter the result with a regexp.

And then use egrep -v to filter in a negative way (if it doesn't match stays).

So I guess something like this might do the job:

find . | egrep "*.(jpg|png)$" | egrep -v "*.(sam.png)"

Upvotes: 0

Related Questions