#!/usr/bin/env bash LIMIT=30 DIR="$1" if [ -z "${DIR}" ] then echo "The directory value cannot be empty" exit 1 fi NO="0" NUMBER=$(find ${DIR} -maxdepth 1 -type f |wc -l) echo "The number of files in the directry: ${NUMBER}" if [[ $NUMBER -gt $LIMIT ]] #if number greater than limit then del=$(($NUMBER-$LIMIT)) if [ "$del" -lt "$NO" ] then del=$(($del*-1)) fi echo $del FILES=$(find ${DIR} -maxdepth 1 -type f -printf "%T@ %f\n" | sort | cut -d' ' -f2 | tail -n "$del") echo "${FILES}" fi