Bash – how to delete old files in ftp with bash


I have a backup ftp server and I have a CRON bash sending all backups here.

Right now I have to manually delete all old files, but I would like to automate this process with a CRON bash

The remote FTP does'nt have date on files (I don't know why), but backups are named by date (%Y-%m-%d-%H-%M-%S).

I thought of listing all files in the directory, sorting them by name and deleting all files with index > X but I don't know how to do that

My current code is :

ftp -inv $ftphost << EOF
user $ftpuser $ftppass
put $savepath.bz2 mysql/"$dbname"/"$datum".sql.bz2

Best Answer

This solution certainly isn't waterproof but if your backup job runs daily and you want to keep 1 month worth of backups you could delete the file that is one month old with something like:

old=$(date -d 'now -1 month' +'%Y-%m-%d')

And then in the FTP section

mdelete mysql/"$dbname"/"$old"*.sql.bz2

Depending on the FTP server you mau need to toggle glob first. The mls command should tell you whether or not the file name expansion works.