How to bulk-copy images only of certain size in Linux

Updated: February 3, 2021

Behold, an interesting problem - or requirement, if you will. You have a folder full of images, say dozens or hundreds. And they are of different sizes, say height of 480 or 600 or 1024 pixels, and width of 200, 320 or 9000 pixels, and you would like to copy only high-quality, large-size images out of this folder into a separate location. Doing this manually can be a chore.

In this article, I'd like to show you a relatively simple command you can run in a terminal window, which will let you filter out your images by size, and then only copy those with an attribute that you like. Now, please be aware that there are dozens of different ways you can accomplish this, and my solution is no way unique or comprehensive (for every possible usecase), but it should do the trick just fine. Let's begin.

Tools of the trade

The only application you will need, in addition to the various default Linux command-line tools, is ImageMagick, which is a bundle/suite of image-processing utilities. It is installed in most distributions out of the box, so you won't need to do anything in most scenarios.

One-liner

Now, the command you need is this:

ls *.FFF | xargs -I{} identify -format '%i %w %h\n' {} | grep -w XXX | awk '{print $1}' | grep -v ^$ | xargs -I cp {} "destination folder"

What do we have here?

Here's the breakdown of different actions:

file1.jpg 640 480
file2.jpg 320 200

cp: target './file.jpg' is not a directory

Two-liner

The above should work great, but if you encounter an issue due to the delicate differences in how tools parse filenames, empty lines and newline characters, you can then simplify the one-line into a two-liner, where you first write the results into a text file, and then parse the text file for copy:

ls *.FFF | xargs -I{} identify -format '%i %w %h\n' {} | grep -w XXX | awk '{print $1}' | tee list.txt

cat list.txt | grep -v ^$ | xargs -I cp {} "destination folder"

For instance, we copy all the PNG images with either dimension set to 800 px:

ls *.png | xargs -I{} identify -format '%i %w %h\n' {} | grep -w 800 | awk '{print $1}' | tee list.txt

cat list.txt | grep -v ^$ | xargs -I cp {} ./dir-only-big-files

Conclusion

There. Now, I am certain you can propose a dozen different methods and tweaks that will accomplish the same, perhaps even more elegantly than what I wrote. If so, please email me with your suggestions, and I might add them to this tutorial. The purpose of this article is not to be the holy grail of scriptology, but a simple solution for a common need. And it does the job well.

Anyway, if you have tons of images, photos, screenshots, whatever, of varying sizes and attributes, you can use the Linux command-line to filter them out in a clever way, plus the ImageMagick identify command. We're even talking things like size, color mapping and more. Noice. Hopefully, you will find this rather useful. Take care.

Cheers.

You may also like: