0

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
ilernet2
  • 115
  • 1
  • 1
  • 7
  • Does this answer your question? [Delete all but the most recent X files in bash](https://stackoverflow.com/questions/25785/delete-all-but-the-most-recent-x-files-in-bash) – ceving Oct 02 '20 at 08:04
  • I need to delete files from ftp. thxs – ilernet2 Oct 02 '20 at 16:03
  • Then look here: https://stackoverflow.com/questions/11203988/linux-shell-script-for-delete-old-files-from-ftp – ceving Oct 04 '20 at 11:32

2 Answers2

2

One option is to use a find command to catch file older than 7 days and delete it. So it give something like this :

find . -type f -name ".*-backup.tar.gz" -mtime +7 -exec rm {} \;

You can add this line in your script If you want to test first remove replace the exec part by print to display the files catched :

find . -type f -name ".*-backup.tar.gz" -mtime +7 -print
YLR
  • 1,503
  • 4
  • 21
  • 28
  • 6
    Two alternatives to `-exec rm {} \;` are: 1) `-exec rm {} +` to issue one single `rm` instead of spawning one per file. 2) Use `-delete` instead of `-exec ...`. These options may not be available everywhere though. – Ted Lyngmo Oct 02 '20 at 07:54
0

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 !

Soumendra Mishra
  • 3,483
  • 1
  • 12
  • 38