Script para borrar archivos de mas de “N” dias o meses

166

Shell script que elimina directorios con mas de 10 dias de antiguedad

#!/bin/sh -x   
#
# Shell script to backup clean stored at /cdrani/Scripts-Cdrs/ directory.
# ————————————————————————-
# Created by: Lic. Domingo Varela Yahuitl
# Date: 24/May/09
# Probe: True
# Modified: 24/May/09 18:40
# Last Modify: 24/May/09 19:53
#
#

 
# Settings
FIND=”/usr/bin/find”
# Backup dir path
CFTPANI01=”/cdrani/ftpani01/cdranimas”
#Deleting directory files older than 10 days
DAYS=14
# Logfiles
LOGFILE=/cdrani/Logs/delete-directory-log.`date +%y-%m-%d-%H:%M`
# String to search
STRING_DIR=”200*”
# Delete directories
DEL_COUNTER=/cdrani/Logs/D_Counter.log.`date +%y-%m-%d-%H:%M`

echo “—————————————————————————-” >> $LOGFILE
echo “Iniciando la busqueda de directorios con mas de ‘$DAYS’ dia(s) de antiguedad … OK” >>$LOGFILE
echo “—————————————————————————-” >>$LOGFILE

# Question if  exist  directory …
if [ `$FIND ${CFTPANI01} -name ${STRING_DIR} -type d -mtime +${DAYS} -print | wc -l` -gt 0 ]; then

# start cleaning
echo “‘$FIND’ ‘${CFTPANI01}’ -name ‘${STRING_DIR}’ -type d -mtime +’${DAYS}’ | sort -n | xargs rm -fr {} \;” >> $LOGFILE
${FIND} ${CFTPANI01} -name ${STRING_DIR} -type d -mtime +${DAYS} | sort -n  >> $LOGFILE
${FIND} ${CFTPANI01} -name ${STRING_DIR} -type d -mtime +${DAYS} | sort -n  >> $DEL_COUNTER
${FIND} ${CFTPANI01} -name ${STRING_DIR} -type d -mtime +${DAYS} | sort -n | xargs rm -fr  {} \;

echo
echo “`wc -l $DEL_COUNTER | awk ‘{print $1}’` Directorio(s)”
echo
echo “‘`cat $DEL_COUNTER`'”
echo
echo “que ha(n) sido eliminado(s) del directorio ‘$CFTPANI01’ … OK “
else
# Directory no found
   echo >> $LOGFILE
   echo “No existe(n) directorio(s) ‘$STRING_DIR’ en el directorio ‘$CFTPANI01’ con mas de ‘$DAYS’ de antiguedad … OK” >> $LOGFILE
   echo >> $LOGFILE
   echo
fi

#
#End shell script
#