Code Review Videos > How I Fixed > How To Get File Name And Image Dimensions Using Linux CLI

How To Get File Name And Image Dimensions Using Linux CLI

A very specific issue today, but one that I found quite a nice solution for – albeit with the need to install some extra things. The issue I faced was that I have a large number of directories, each containing 10-15 images. For each directory I needed the file name, and image dimensions for every image. Sounds like a job for the CLI.

I’ll be completely honest, this problem was a direct result of doing some web scraping.

As such I can’t share the real names of the directories and files, as it would almost immediately give away the game, and this was a paid project so … yeah. It is what it is.

But the actual filenames and folder names doesn’t really matter. The concept is what counts here.

So let’s dive in.

The Setup

We have a directory that contains 1 or more sub directories.

Each sub directory contains 1 or more images.

In my case, the images could be of varying type:

  • jpg
  • jpeg
  • png

Etc.

That is important, but also really trivial to solve. We will begin only by looking at jpg and then add in the others.

my-first-directory tree .
.
├── my-first-directory-10.jpg
├── my-first-directory-11.jpg
├── my-first-directory-12.jpg
├── my-first-directory-13.jpg
├── my-first-directory-14.jpg
├── my-first-directory-15.jpg
├── my-first-directory-1.jpg
├── my-first-directory-2.jpg
├── my-first-directory-3.jpg
├── my-first-directory-4.jpg
├── my-first-directory-5.jpg
├── my-first-directory-6.jpg
├── my-first-directory-7.jpg
├── my-first-directory-8.jpg
└── my-first-directory-9.jpg

0 directories, 15 filesCode language: CSS (css)

OK, simple starting point.

We’re in a directory called my-first-directory.

I’ve run the tree command, and it’s listed out the files in the directory.

The Command – One Directory

We will use imagemagick to help with this.

On Ubuntu, it’s easy to install:

sudo apt-get update
sudo apt-get install imagemagickCode language: JavaScript (javascript)

ImageMagick gives us a bunch of handy commands for working with images, but the one we will use to get the file name and file dimensions is identify.

Here’s the basic command:

identify -format '%f %wx%h\n' /path/to/directory/*Code language: JavaScript (javascript)

Note the * at the end of the line is important. If you don’t add the star you will get something like:

identify-im6.q16: no decode delegate for this image format `' @ error/constitute.c/ReadImage/575.Code language: JavaScript (javascript)

It’s also possible you get that error if you don’t have the right delegate installed for the specific image format of the file you are trying to identify.

To fix this error, you can try installing the necessary delegate for the image format you are working with. For example, if you are trying to identify a PNG image, you may need to install the libpng library.

On Ubuntu or Debian-based systems, you can install the necessary delegate library using the following command:

sudo apt-get install libpng-dev

# or for jpg

sudo apt-get install libjpeg-devCode language: JavaScript (javascript)

Anyway, if you run that command on your current directory, you should see something like this:

➜  my-first-directory identify -format '%f %wx%h\n' ./*

my-first-directory-10.jpg 1600x1064
my-first-directory-11.jpg 1600x1064
my-first-directory-12.jpg 1064x1600
my-first-directory-13.jpg 1600x1064
my-first-directory-14.jpg 1064x1600
my-first-directory-15.jpg 1600x1064
my-first-directory-1.jpg 1600x1064
my-first-directory-2.jpg 1064x1600
my-first-directory-3.jpg 1600x1064
my-first-directory-4.jpg 1064x1600
my-first-directory-5.jpg 1064x1600
my-first-directory-6.jpg 1064x1600
my-first-directory-7.jpg 1600x1064
my-first-directory-8.jpg 1064x1600
my-first-directory-9.jpg 1600x1064Code language: JavaScript (javascript)

Pretty neat.

Change up the format flag to get the output you want. E.g. as a CSV-esque output:

-format '%f,%wx%h\n'

...

my-first-directory-8.jpg,1064x1600
my-first-directory-9.jpg,1600x1064Code language: JavaScript (javascript)

Very good.

The Command – Multiple Directories

We have a way to get a list of formatted file names and image dimensions for a single directory.

Combining this with another Linux CLI command, we can now loop through a directory and repeat for all sub-directories.

➜  my-top-level-directory find . -type f -iname "*.jpg" -print0 | xargs -0 -I{} identify -format '%f %wx%h\n' "{}"

directory-1-409.jpg 642x950
directory-1-361.jpg 642x950
directory-1-354.jpg 642x950
directory-1-395.jpg 642x950
directory-1-352.jpg 642x950
directory-1-351.jpg 642x950
directory-1-472.jpg 642x950
directory-2-6964.jpg 640x960
directory-2-004.jpg 1280x800
directory-2-6822.jpg 640x960
directory-2-6857.jpg 640x960
directory-2-6925.jpg 640x960
directory-2-6934.jpg 640x960
directory-2-6852.jpg 640x960
directory-2-005.jpg 1280x800
directory-2-6870.jpg 640x960
directory-2-6960.jpg 640x960
directory-2-001.jpg 1280x800
directory-2-6954.jpg 640x960
directory-2-002.jpg 1280x800
directory-3-001.jpg 1280x800
directory-3-002.jpg 1280x800
directory-3-282.jpg 960x640
directory-3-225.jpg 960x640
directory-3-296.jpg 960x640
directory-3-322.jpg 960x640
directory-3-315.jpg 960x640Code language: JavaScript (javascript)

This is pretty useful.

The important part of this is that the find command needs to be run from the top level directory. That’s the one that contains all your sub-directories.

find . means “find in the current folder”.

You could spell out the full / absolute path to your current directory also.

This is kinda good, but including the directory name, or full file path to each image would be very handy, I feel.

Depending on your needs, you may either want a relative path:

find . -type f -iname "*.jpg" -print0 | xargs -0 -I{} identify -format '%d/%f %wx%h\n' "{}"

# e.g.

./path/to/directory-1-409.jpg 642x950
./directory-2/directory-2-6900.jpg 640x960
./directory-2/directory-2-003.jpg 1300x866Code language: PHP (php)

Or an absolute path:

find . -type f -iname "*.jpg" -print0 | xargs -0 -I{} sh -c 'echo "$(realpath "{}") $(identify -format "%wx%h" "{}")"'

# e.g. 

/home/chris/Development/nefarious-scraper/path/to/directory-1-409.jpg 642x950
/home/chris/Development/nefarious-scraper/directory-2/directory-2-6900.jpg 640x960
/home/chris/Development/nefarious-scraper/directory-2/directory-2-003.jpg 1300x866Code language: PHP (php)

That should cover most use cases for this sort of thing, I feel.

It certainly covered mine.

Identify Multiple File Types

It may be that you need more than one type of file to be matched here.

I did.

I needed jpg and png.

After all this, the fix here is super easy. Not sure why I saved it this long. Such a tease:

find /path/to/directory -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" \) -print0 | xargs -0 -I{} identify -format '%f %wx%h\n' "{}"Code language: JavaScript (javascript)

And that should be everything you need.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.