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

I have similar situation and I see this is not answered fully yet. I understand that those 3 bullets are the milestones of the script:

  • 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

and this is obvious. But can we get a script which is doing them?

My files are having a files names containing date and time when they had been generated and are uploaded to remote FTP server:


Position of the date and time (e.g. 20160313102903) in the file name is static. Of course time stamps of those files are also valid.

My goal is to delete files older than X days using a script which should run in cron on a linux machine where I can use only curl or lftp.

  • I can't use any other protocol except ftp to access FTP server (no sftp, no ssh)

  • I can't use date command with "X days ago" nor "--iso" syntax as that linux machine is running BusyBox v1.20.2 (2015-01-30 16:18:45 WET) which doesn't understand human date references

  • I can't create a temporary subfolders at that FTP server

I have been searching scripts which should lead me to the solution but because I'm linux noob I'm progressing very slowly and so far I have found only:

  • I'm able to list the date of X days ago: date -d "@$(($(date +%s) - 86400 * $XDays))"
  • I'm able to list all the files from FTP server: curl ftp://[FTP_IP]/folder/subfolder/* --user MyUser@[FTP_IP]:[MyUserPassword] --list-only"

Real script is very welcome.

share|improve this answer
Welcome to ServerFault! Instead of adding your question on this post, it would be better to start your own, separate question, perhaps with a reference to this post, rather than hijacking an existing question. Posted answers should address the original question, not add on similar questions. – Castaglia Mar 13 at 15:58

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.