Search code examples
linuxbashfilexargslftp

lftp delete multiples files with Bash


I try to create a script who delete all the olds files except the three more recent files on my backup directory with lftp.

I have try to do this with ls -1tr who return all the files in ascending date order, and after I do a head -$NB_BACKUP_TO_RM ($NB_BACKUP_TO_RM is the numbers of files that I want to delete in my lists), this two commands return the correct files.

After this I want to remove all of them, so I do a xargs rm --, but Bash returns that the files don't exist... I think this command is not running into the remote directory, but in the local directory, and I don't know what I can do for delete this files (of my return lists).

Here is the full code:

MAX_BACKUP=3
NB_BACKUP=$(lftp -e "ls -1tr $REMOTE_DIR/full_backup_ftp* | wc -l ; quit" -u $USER,$PASSWORD $HOST)

if (( $NB_BACKUP > $MAX_BACKUP ))
then
   NB_BACKUP_TO_RM=$(($NB_BACKUP-$MAX_BACKUP))
   REMOVE=$(lftp -e "ls -1tr $REMOTE_DIR/full_backup_ftp* | head -$NB_BACKUP_TO_RM | xargs rm -- ; quit" -u $USER,$PASSWORD $HOST)
   echo $REMOVE
fi

Have you an idea of the problem? How can I delete the files of my lists (after ls -1tr $REMOTE_DIR/full_backup_ftp* and head -$NB_BACKUP_TO_RM)

Thanks for your help


Solution

  • Looks xargs is unknown cmd for lftp after man lftp. And xargs rm is deleting local files not remote files.

    so please use xargs as below, it works for me.

    lftp -e "ls -1tr $REMOTE_DIR/full_backup_ftp*; quit" -u $USER,$PASSWORD $HOST | head -$NB_BACKUP_TO_RM | xargs -I {} lftp -e 'rm '{}'; quit' -u $USER,$PASSWORD $HOST