#!/bin/bash MAXN=${2:-30} num=$(ls -1rt | grep -- $1 | wc -l) if [[ $num -lt $(($MAXN-1)) ]]; then exit 1 fi num=$(($num-$MAXN)) for fil in $(ls -1rt | grep -- $1 | head -n $num); do rm -v $fil done