Thursday, 11 March 2010

delete all the images that are not in a specific format

Ok, I'm writing this because it may be useful to me again in the future, as well as to somebody else.

Problem: suppose you have a bunch of jpeg files, with names such as .jpg. Then, you open some of these, and unfortunately enough some of them aren't actually JPEG, but say PNG with the wrong extension. You may want to move them, rename them, or do something to fix the problem (many image viewers won't complain, but some will). In my case I just wanted to remove them.

The command uses ImageMagick, it is for linux and it goes as
identify -format "%f %m\n" * | grep -v JPEG | awk '{ print $1 }' | xargs rm
Identify is an ImageMagick program that retrieves information about the image file, the -format option tells what to print (filename and format, as detected by the magic number).

The grep command takes all the lines that do *not* match "JPEG".

the awk takes the first word in the line (the filename).

The xargs concatenates the word to the command "rm" (REMOVE, again, pay attention!).

1 comment:

adin said...


You can do it with awk in a simpler way:

identify -format "%f %m\n" * | awk '$2 !~ /JPEG/ {system("rm " $1)}'

just change the format you don't want to delete