Skip to content
Advertisement

Bash script – delete old files

I have a script that uploaded a compressed file to an ftp, it is the code that I show below.

This code works correctly, but I would like to adapt it so that once the file is uploaded, it deletes ftp files older than a week.

#!/bin/sh
HOST='xxx'
USER='xxx'
PASSWD='xxx'
DAY=`date +"%d%m%Y_%H%M"`

cd /temp
rm -fr backup
mkdir backup
cd backup

 
export GZIP=-9
tar -czvf $DAY-backup.tar.gz  --exclude="*/node_modules/*" /var/www/html/cars


FILE=$DAY-backup.tar.gz

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
binary
put $FILE
quit
END_SCRIPT
exit 0

Advertisement

Answer

You can try this solution:

# Purpose: This step is used to Purge 7 days old files
export PROJECT_LOG="${PROJECT_HOME}/log";
export APP_MAINT_LOG="APP.log"
export LOG_RETAIN_DUR=7
echo "Maintenance Job Started" > "${APP_MAINT_LOG}"
echo "=========================================================================" >> "${APP_MAINT_LOG}"
echo "${LOG_RETAIN_DUR} Day(s) Old Log Files..." >> "${APP_MAINT_LOG}"
echo "=========================================================================" >> "${APP_MAINT_LOG}"
find "${PROJECT_LOG}" -mtime +"${LOG_RETAIN_DUR}" -type f -exec ls -1 {} ; >> "${APP_MAINT_LOG}"
#find "${PROJECT_LOG}" -mtime +"${LOG_RETAIN_DUR}" -type f -exec rm -rf {} ;
echo "=========================================================================" >> "${APP_MAINT_LOG}"
echo "Maintenance Job Completed" >> "${APP_MAINT_LOG}"
cat "${APP_MAINT_LOG}"

Note: I have commented the Remove file line, so that you can check and run !

Advertisement