Shagshag
11/15/2017 - 3:11 PM

Delete files older than X days on FTP. use curlftpfs

Delete files older than X days on FTP. use curlftpfs

#!/bin/bash    

ftpuser="x"
ftppass="x"
ftpsite="x"
ndays=10

# https://stackoverflow.com/a/34676160/2530962
# the directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# the temp directory used, within $DIR
# omit the -p parameter to create a temporal directory in the default location
WORK_DIR=`mktemp -d -p "$DIR"`

# check if tmp dir was created
if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
  echo "Could not create temp dir"
  exit 1
fi

# deletes the temp directory
function cleanup {      
  rm -rf "$WORK_DIR"
  echo "Deleted temp working directory $WORK_DIR"
}

# register the cleanup function to be called on the EXIT signal
trap cleanup EXIT

# implementation of script starts here
curlftpfs ftp://$ftpsite -o user="$ftpuser:$ftppass" $WORK_DIR
find $WORK_DIR/* -mtime +$ndays -delete
fusermount -u $WORK_DIR/