Linux shell script for delete old files from ftp Linux shell script for delete old files from ftp shell shell

Linux shell script for delete old files from ftp


This is a script I wrote to remove any files on a remote ftp site older than 7 days. It works by retrieving a listing of the directory, parsing the modified date, and then re-connecting to delete any files older than ndays.

I suspect that the numbers hard-coded into the loop (element date) may change depending on the setup of your system. The return formatting of the ls command is dependent on the local system settings.

Assuming your backups are every day, then setting ndays to 10 might solve your problem.

#!/bin/bash# get a list of files and dates from ftp and remove files older than ndaysftpsite="ftp.yourserver.com"ftpuser="loginusername"ftppass="password"putdir="/public_ftp/admin/logs"ndays=7# work out our cutoff dateMM=`date --date="$ndays days ago" +%b`DD=`date --date="$ndays days ago" +%d`echo removing files older than $MM $DD# get directory listing from remote sourcelisting=`ftp -i -n $ftpsite <<EOMYF user $ftpuser $ftppassbinarycd $putdirlsquitEOMYF`lista=( $listing )# loop over our filesfor ((FNO=0; FNO<${#lista[@]}; FNO+=9));do  # month (element 5), day (element 6) and filename (element 8)  #echo Date ${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]}          File: ${lista[`expr $FNO+8`]}  # check the date stamp  if [ ${lista[`expr $FNO+5`]}=$MM ];  then    if [[ ${lista[`expr $FNO+6`]} -lt $DD ]];    then      # Remove this file      echo "Removing ${lista[`expr $FNO+8`]}"      ftp -i -n $ftpsite <<EOMYF2       user $ftpuser $ftppass      binary      cd $putdir      delete ${lista[`expr $FNO+8`]}      quitEOMYF2    fi  fidone


This one is dealing with sftp and has proper date check as the script from @Graeme is only working within one month:

#!/bin/bash# get a list of files and dates from ftp and remove files older than ndaysftpsite="sftp -b-  -oIdentityFile=<KEYFILE> -oPort=<PORT>  <USER>@<HOST>"putdir="/data"ndays=19# work out our cutoff dateMM=`date --date="$ndays days ago" +%b`DD=`date --date="$ndays days ago" +%d`TT=`date --date="$ndays days ago" +%s`echo removing files older than $MM $DD# get directory listing from remote sourceecho "cd $putdirls -l"|$ftpsite >dirlist# skip first three and last line, ftp command echolisting="`tail -n+4 dirlist|head -n-1`"lista=( $listing )# loop over our filesfor ((FNO=0; FNO<${#lista[@]}; FNO+=9));do  # month (element 5), day (element 6) and filename (element 8)  # echo Date ${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]}          File: ${lista[`expr $FNO+8`]}  fdate="${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]} ${lista[`expr $FNO+7`]}"  sdate=`date --date="$fdate" +%s`  # check the date stamp  if [ $sdate -lt $TT ]  then      # Remove this file      echo "$MM $DD: Removing  ${lista[`expr $FNO+5`]} /  ${lista[`expr $FNO+6`]} / ${lista[`expr $FNO+8`]}"      $ftpsite <<EOMYF2      cd $putdir      delete ${lista[`expr $FNO+8`]}      quitEOMYF2  fidone


It has passed several years, and while Graeme's answer helped me but I faced some problems with it and here is how to fix them.

Sometimes the sentence like Trying aaaa:bbb:ccc:dddd::0... is at first line of listing varable. You have to remove it.

rem="Trying aaaa:bbb:ccc:dddd::0...";rep="";listing=${listing/$rem/$rep}

Also jsan commented he faced other issue: "value too great for base (error token is "08")" when $DD is 0x it is interpreted as x in octal (problem for 8 and 9)

I faced same problem. So you better make sure there is no nun-numeric characters in $MM or $DD and then remove the 0 from beggining. Like 08 should become 8.

so before looping on files, add this code:

MM=$(tr -dc '0-9' <<< $MM)DD=$(tr -dc '0-9' <<< $DD)MM=${MM#0}DD=${DD#0}

First two lines remove non-numeric characters and second last lines fix the problem with 08