Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to find all JPG images within a folder with subfolders that have either width or hight below 300px.

This way I want to detect old thumbnails and delete them.

For sure I can find all images using find:

find . -iname "*.jpg" -type f | ...

But what follows after the pipe? Which package can I use to detect picture's attributes?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

You can use identify from imagemagick, and you can use the following command:

find . -iname "*.jpg" -type f -exec identify -format '%w %h %i' '{}' \; | awk '$1<300 || $2<300'

the use of -exec <command> '{}' \; makes sure that your filename can have spaces in them, alternatively you can use

find . -iname "*.jpg" -type f | xargs -I{} identify -format '%w %h %i' {} | awk '$1<300 || $2<300'

where the -I{} takes care of the same thing.

What I like about identify is that you can specify the output format; in this case '%w %h %i' which gives the width, height and full pathname of the image. Then the awk expression only keeps those lines for which the image is smaller than the desired size.

Example of the output:

64 64 ./thumbsup.jpg
100 150 ./photomin.jpg

Edit: If you want the filenames only (for piping to rm for instance), simply change $line in the awk statement to $3, then it will only print the third column.

share|improve this answer
    
The awk part can be made shorter: | awk '$1<300||$2<300' or | awk '$1<300||$2<300{print $3}' (when only the 3rd column is needed). –  har-wradim Oct 6 at 18:21
    
@har-wradim Thanks, nice suggestion! –  Gerhard Burger yesterday

The identify command from the imagemagick package does what you want:

$ identify abc.jpg
abc.jpg JPEG 1952x3264 1952x3264+0+0 8-bit DirectClass 1.111MB 0.000u 0:00.000

Again, you would need to then use grep to sort out the image size.

However, I suspect that unless you have a very wide range of image sizes, it would be easier just to use find to remove JPEG files below a given size:

find -iname '*.jpg' -size -10k -delete

(Worth running without -delete first to check it doesn't find things you want to keep - it won't prompt you before deletion otherwise).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.