nullByteMe
nullByteMe

Reputation: 6391

How to delete a directory based on date and time

The below script will be called from a cronjob and my desire is to remove directories older than 10 mins of the current time.

I'm having trouble with the following in my script:

This is what I have:

#!/bin/sh

i=1
homeDirData="/home/user"$i"/dirToClean/"

while [ -d $homeDirData ]
do
   echo "Checking user"$i""

   for oldDir in $(find . -type d)
   do
      oldDirDay=$(ls -l $oldDir | awk '{print $7}')
      oldDirTime=$(ls -l $oldDir | awk '{print $8}' | tr -d ':')

      curDay=$(date +%d%t%R | awk '{print $1}')
      curTime=$(date +%d%t%R | awk '{print $2}')

      if [ $oldDirDay -lt $curDay ] && [ $oldDirTime -lt $(($curTime+600)) ]
      then
         find $homeDirData -type f -exec shred -f --remove {} \;
         rm -rf $homeDirData
      else
         echo "Nothing to remove"
      fi

      ((i+1))
   done
done

Error

Issue

Upvotes: 0

Views: 2714

Answers (1)

leshats
leshats

Reputation: 36

You can use something like this:

find $homeDirData -type d -mmin +10 -print0 | xargs -0 rmdir

Upvotes: 2

Related Questions