2

I'm kinda new to shell scripts and I'm struggling to get only the information I need from a command.

I'm executing this script:

#!/bin/bash
myfunction(){
   find . -name '*' -exec file {} \; | grep "image"
}
myfunction

Basically, what I need is the list of the names of the images on my directory and the data about their resolution, but I get a lot of data that are useless to me:

./image222s.jpeg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 554x554, components 3
./fruta-png.png: PNG image data, 1400 x 1501, 8-bit/color RGBA, non-interlaced
./ddd.jpeg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 540x568, components 3
./images.jpeg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 452x678, components 3

I'd like to know how can I reduce the output to only the file name and its resolution. Something like:

image.jpeg: 452x678
image222.jpeg: 554x554

Thanks!

1
  • -name '*' is redundant - however you might want to add a -type f Sep 18, 2019 at 13:50

1 Answer 1

1

If you can use identify tool from imagemagick, you can pass a -format string to only print the needed information:

Install imagemagick if necessary:

sudo apt-get install imagemagick

Then run:

find . -type f -exec identify -format "%i: %P\n" {} \; 2>/dev/null

(or + instead of \; which will be faster but will print at the end only).


If you have a lot of non-image files matching your find, you might want to filter these first because file will be faster than identify:

find . -type f -exec sh -c 'file "$1" | grep -q image' find-sh {} \; -exec identify -format '%i: %P\n' {} \;

Note, that file might filter some file formats it doesn't know about, that identify could do, e.g. RAW images.

2
  • I haven't timed it, but it might be more efficient to filter the inputs first e.g. find . -type f -exec sh -c 'for f; do file "$f" | grep -q 'image' && identify -format "%i: %P\n" "$f"; done' sh {} + Sep 18, 2019 at 13:48
  • Depends on the relation between non-images and images. A short test on my Pictures directory says that direct identify is faster. For general content folder filtering might be better. But it might also fail for some types that identify can do, e.g. Raw images.
    – pLumo
    Sep 18, 2019 at 13:50

You must log in to answer this question.

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