summaryrefslogtreecommitdiff
path: root/bin/prune.sh
blob: 22f32679e6a934a14167e5c6b976f79a143e12f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/sh -e

dir=$1
max=$2

usage()
{
    echo "usage: $0 <dir> [max items]"
    exit 1
}

[ -z "$dir" ] && usage
[ -d $dir ] || usage
[ -z "$max" ] && max=10


num=`ls $dir | wc -l`
echo num $num

if [ $num -gt $max ]; then
    kill=$(($num - $max))
    echo will remove $kill

    # keep biggest and smallest 2
    ( cd $dir && ls -S | tail -n +2 | head -n -2 | sort | head -n $kill | xargs rm )
fi