Listing 12
#
# fleave.sh: leave n newest files in current directory, remove others
# usage: fleave.sh dirname #-to-leave
#
[ $# -ne 2 ] && echo "usage: $0 <dir> <# to leave>" && exit 1
[ ! -d "$1" ] && echo "$0: $1 is not a directory" && exit 1
[ $2 -lt 5 ] && echo "$0: cannot leave less than 5 files." && exit 1
cd $1
n=$2
temp=`tmpname flv`
ls -t | awk '{ if (NR > '$n') print "rm " $1}' >$temp
sh <$temp
rm $temp
|