Skip to main content
6 of 6
added 7 characters in body
Praveen Kumar BS
  • 5.3k
  • 2
  • 11
  • 16

Below script will work fine for you

#!/bin/bash
count=`find . -maxdepth 1 -type d | wc -l`
if [[ $count > 20 ]]
then
  under_7days=`find . -maxdepth 1 -type d -daystart -mtime -7 | wc -l`
  if [[ $under_7days >= 20 ]]
  then
    find . -maxdepth 1 -daystart -mtime +7 -exec rm -rvf {} \;
  else
    echo "Since total directories which created under 7 days is $under_7days"
  fi
elif [[ $count <= 20 ]]
then
  echo "Number of folders exsists in the path is $count"
fi

I have used maxdepth 1 so it will consider only the directories in the current path.

Praveen Kumar BS
  • 5.3k
  • 2
  • 11
  • 16