diff --git a/cleanup-files.sh b/cleanup-files.sh new file mode 100755 index 0000000..9fb2e99 --- /dev/null +++ b/cleanup-files.sh @@ -0,0 +1,29 @@ +#!/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