Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i've got a remote FTP server where i store some backups via cronjob.

The Problem is that i only got a little amount of space so im doing incremental backups. I want to keep the backups about 14 days.

I don't have any other access to the server except FTP and i don't now how to delete files older than x days. Every file got a name with a date in it

yxzNamezxy-date-y-m-d.tar.bz2 (datev-20100111.tar.bz2(

Hope to get some help here.

Cheers and thanks for the hopefully coming answeres,



I'm trying to use the curl thing which is mantioned in an answere with this part of code

curl ftp://$FTP_SERVER --user $FTP_USER:$FTP_PASS --list-only > files.tmp
declare -a aFiles
let iCount=0
exec < files.tmp
while read sLine

echo -e "\n Files: \n\n\n"
echo ${aFiles[@]}

echo -e "\nfor ...\n\n\n"
for sFile in $aFiles
        echo -e "\nFile:" $sFile

but the secound loop doesn't give me more than 1 filename



share|improve this question
See my edited answer for help with the second loop. – Dennis Williamson Jan 14 '10 at 15:37
up vote 2 down vote accepted

You could send a series of commands to lftp and wrap it in a shell script which calculates the filename of the too old files (see the script below).

TODAY=$(date --iso)                   # Today's date like YYYY-MM-DD
RMDATE=$(date --iso -d '6 days ago')  # TODAY minus X days - too old files
FTPPW=password                        # Better load this from an encrypted file
LFTP=/usr/bin/lftp                    # Path to binary
TMPDIR=$(mktemp -d)                   # Your archives are here

# CAUTION: mput -E deletes local files after upload
echo -n "Uploading files via FTP... "
cd backups/${HOSTNAME}
mkdir ${TODAY}
cd ${TODAY}
mput -E ${TMPDIR}/*
cd ..
rm -rf ${RMDATE}
echo "Done."

Make sure nobody execpt root can read this script or put the credentials somewhere else. TMPDIR is the directory where the backups reside locally. Of course you need to edit the obivous parts.


share|improve this answer
Thank i'll look into it by Tomorrow or so, if it works that would be my prefered answer – user25634 Jan 13 '10 at 7:39
I can send you my script if you like. – PEra Jan 13 '10 at 18:54
That would be great, Cheers – user25634 Jan 14 '10 at 8:41
That part seems to work quite well. Thanks a lot PEra – user25634 Jan 21 '10 at 9:20

You should be able write a script to:

  • Get the list of files using the --list-only option of curl
  • Parse the list for the files you want to delete
  • Use the --quote or --request option to send a rm or DELE command with the selected filenames


Your second loop is referring to $aFiles as a scalar. You need to reference it as an array like this:

for sFile in ${aFiles[@]}
        echo -e "\nFile:" $sFile

Also, you don't need a temp file. You should be able to pipe curl into the while.

share|improve this answer
Well yes im still trying to figure out how exactly, im stucking with the part (edited in the original post) it keeps not giving me the right files in the 2nd for loop – user25634 Jan 14 '10 at 15:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.