2

I have a server backup of a WordPress site that has about 1000 images in the uploads directory. When an image was uploaded through WordPress it would create multiple sizes of the image such as:

foobar.jpg
foobar-170x170.jpg
foobar-250x250.jpg

I'm familiar with using find to move all files from a directory and its subfolders with something like find . -mindepth 3 -type f -print -exec mv {jpe?g|png|gif} . \; but I was curious to know if an exclusion along the lines of "don't move this" can be added. I'd like to give a pattern and then exclude filenames containing that pattern. Something like !(\d{2,5}x\d{2,5}\.(jpe?g|png|gif)).

Ideally, I would like to do this in a single command without running it in a loop or using rm to delete the files after I've moved them all. Any better suggestions to reduce multiple terminal commands would be great.

0

1 Answer 1

2

You can use find's -not or ! expressions:

   -not expr
          Same as ! expr, but not POSIX compliant.

   ! expr True  if  expr  is false.  This character will also usually need
          protection from interpretation by the shell.

So, to move foobar.jpg but not the other two, you could use:

find . -mindepth 3 -type f -regex '.*.jpg\|.*.jpeg\|.*png\|.*gif' \
       -not -regex '.*[0-9]+x[0-9]+.*' -exec mv {} . \;

You can also make it match only 2-5 digits on either side with

find . -mindepth 3  -type f -regextype posix-egrep \
       -regex '.*.jpg|.*.jpeg|.*png|.*gif' -not -regex '.*[^0-9][0-9]{2,5}x[0-9]{2,5}.*' 
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .