Ruben Rizzi
Ruben Rizzi

Reputation: 352

Deleting all files except the ones which match a regex

I have a set of images generated by wordpress, and I need to get rid of them (example of one generated set):

But I need to keep the following (the last two in this case):

I am trying to do that using:

find /myPathToImages -type f -name '*[0-9]x*[0-9].jpg' -delete

But I also get rid of the 1024x683 version. How can I filter it out?

UPDATE:

The regex must keep into account also a vertical image, something like this:

The logical rule is deleting all the images except the original ones and the cropped ones having either width or height = 1024

Upvotes: 0

Views: 377

Answers (3)

Pedro Lobito
Pedro Lobito

Reputation: 98901

There's a pattern on the filenames to delete, they all have 3digits x 3digits , based on that, you can use :

.*-[0-9]{3}x[0-9]{3}\.jpg

Regex Demo and Explanation

Upvotes: 0

klutt
klutt

Reputation: 31306

Why make it harder than it is? You obviously have a way of finding the images you want to keep. Move them to a separate directory. If needed, do it automatically with something like this:

mkdir keep
for file in $(ls | grep <regex>); do mv $file keep; done
rm *
mv keep/* .
rm -rf keep

Maybe not the most elegant solution, but it works and the general idea is very versatile.

Upvotes: 1

kaldoran
kaldoran

Reputation: 855

Juste use this one :

find /myPathToImages -type f -name '*-[0-9]{3}x[0-9]{3}.jpg' -delete

This will delete all image that looks like that 3 Digit x 3 Digit.

As @Pedro Lobito point out, you need to add - before the pattern in order to not match 4 Digit length. Cause due to the * It would have considered : * followed by 3 digit.

Upvotes: 0

Related Questions