Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a large number (more than 400) of directories full of photos. What I want to do is to keep the larger sizes of these photos. Each directory has 31 to 66 files in it.

Each directory has thumbnails, and larger versions, plus a file called example.jpg

I dispatched the example.jpg file easily with:

rm */example.jpg

I initially thought that it would be easy to delete the thumbnails, but the problem is they are not consistently named. The typical pattern was photo1.jpg and photo1s.jpg. I did rm */photo*s.jpg but it ended up some of the files named photoXs.jpg were actually larger and not smaller. Argh.

So what I want to do is scan each directory for filesize and delete (or move) the thumbnails. I initially thought I'd just ls -R every file and extract the size of each file and save those under a threshold. The problem? In one directory the large will be 1.1 MB and the thumb is 200k. In another the large is 200k and the small 30k.

Even worse, the files really are mostly named photo1.jpg - so simply putting them all in the same folder, sorting by size, and deleting in groups would not work without renaming already, and if it's possible I'd prefer to keep them in their folders.

I was almost resolved to just doing this all manually, but then thought I'd ask here.

How would you do this task?

share|improve this question
    
Is there any relationship between the name of an image and it's thumbnail image or other sized images? –  Marnix A. van Ammers Apr 29 '10 at 15:59

4 Answers 4

up vote 1 down vote accepted

This script makes the assumption that there's a significant gap in the sizes between the groups of smallest and largest files. In particular that the smallest of the large files is at least twice as big as the largest of the small files.

Call the script "imagedirstats" and run it in a loop such as this one:

find /path/to/main/branch -type d | while read subdir; do (cd "$subdir" && ~/bin/imagedirstats ); done

in order to find and delete files in indivdual directories in tree of directories.

Here is the script:

#!/bin/bash
# from http://superuser.com/questions/135951/batch-deletion-of-smaller-files-from-group-of-files-via-unix-command-line
# by Dennis Williamson - 2010-04-29

prevn=1     # prevent division by zero
factor=4    # how close to the largest of the small files to set the threshold, 4 == one fourth of the way above
min=1000    # ignore files below this size

while read n
do
    (( ratio = n / prevn ))
    if (( ratio > 1 && n > min ))
    then
        break
    fi
    if (( n > 0 ))
    then
        prevn=$n
    fi
done < <(find . -maxdepth 1 -name "*.jpg" -printf "%s\n" | sort -n)
# for OS X, comment out the preceding line and uncomment this one:
# done < <(find . -maxdepth 1 -name "*.jpg" | stat -f "%z" | sort -n)

# the following line would be the GNU equivalent using stat(1) instead of printf
# it's included here for reference:
# done < <(find . -maxdepth 1 -name "*.jpg" | stat -c "%s" | sort -n)

(( size = (n - prevn) / factor + prevn ))

echo "Smallest of the large: $n"
echo "Largest of the small: $prevn"
echo "Ratio: $ratio"
echo "Threshold: $size"

if (( ratio < 2 ))
then
    read -p "Warning: ratio too small. Delete anyway? Only 'Yes' will proceed" reply
    if [[ $reply != "Yes" ]]
    then
        echo "Cancelled" >&2
        exit 1
    fi
fi

# uncomment the delete on the following line to actually do the deletion

find . -maxdepth 1 -name "*.jpg" -size -${size}c # -delete

Edit: Moved the warning prompt so useful information would be displayed first. Fixed a missing fi.

Edit 2: Made the two find commands consistent. Added a commented-out variation for OS X. Added information on running the script.

share|improve this answer
    
Saved this as imagedirstats: and ran it with ~/bin/imagedirstats */* and the result is: -bash: /Users/artlung/bin/imagedirstats: Argument list too long ... run with ~/bin/imagedirstats * and I get: find: illegal option -- p find: illegal option -- r find: illegal option -- i find: illegal option -- n find: illegal option -- t find: %s\n: No such file or directory /Users/artlung/bin/imagedirstats: line 38: syntax error: unexpected end of file –  artlung Apr 29 '10 at 15:37
    
The script doesn't take any arguments. It's designed to be run in the current directory or inside a loop that iterates over a set of directories. Also, see where I edited the find command. I forgot to include a -maxdepth argument. –  Dennis Williamson Apr 29 '10 at 16:02
    
What does find --version give you? What Linux distribution (and version)? –  Dennis Williamson Apr 29 '10 at 16:08
    
The reason for the current-directory and maxdepth restrictions is to accommodate the condition you described in your question in which file size ranges overlap across directories. –  Dennis Williamson Apr 29 '10 at 16:11
    
This is what the outer loop might look like: find /path/to/main/branch -type d | while read subdir; do (cd "$subdir" && ~/bin/imagedirstats ); done –  Dennis Williamson Apr 29 '10 at 16:16

If there's some specific cuttoff you can find, for example all large pictures are larger than 200KB, then you can do this:

find */*.jpg -size -200k -delete

You might want to make a backup copy first.

share|improve this answer
    
This is promising. There's not a clear cutoff in size, unfortunately, but this is a nice way to narrow down a majority maybe. Thanks! –  artlung Apr 29 '10 at 14:41

If the file sizes aren't consistent, are the image dimensions?

You could use identify, a tool that comes with ImageMagick, to grab the image dimensions. Through some simple bash scripting, you can work with the images depending on their size.

To grab the width and height of an image with identify:

identify -format '%wx%h' filename

you'll get output like so:

[john@awesome:~]$ identify -format '%wx%h' W4.JPG
1680x1050

You can then use the cut utility to grab the numbers in your script:

[john@awesome:~]$ identify -format '%wx%h' W4.JPG | cut -d'x' -f1
1680
[john@awesome:~]$ identify -format '%wx%h' W4.JPG | cut -d'x' -f2
1050
share|improve this answer
    
Okay, installing imagemagick via brew (on Mac) and will take a look. this is a piece for extracting height. I think there's some consistency in the heights of the thumbnails -- not in the larger ones. Promising! –  artlung Apr 29 '10 at 14:40
    
Even if they aren't exactly consistent, if all thumbnails are less than 200 pixels in height you can do if [ $h -lt 200 ] ... –  John T Apr 29 '10 at 15:02

If you want to do this on the basis of file name, try the following:

find -name '*.jpg' | sed -ne 's:^\(.*\)\.jpg:\1s.jpg$:p' | xargs rm

It will find every .jpg file, put an "s" at the end of the filename (just before the ".") and delete it.

share|improve this answer
1  
The OP said the files are inconsistently named and some of the large files have "s". –  Dennis Williamson Apr 29 '10 at 14:54
    
Thanks petersohn, but DW is right. If they were named like a sane person would name them then it would be much easier to simply delete files matching the pattern and be done. –  artlung Apr 29 '10 at 14:59

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.