简体   繁体   English

lftp 用 Bash 删除多个文件

[英]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.我尝试创建一个脚本,该脚本使用 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.我尝试使用ls -1tr来执行此操作,它按日期升序返回所有文件,并且在执行head -$NB_BACKUP_TO_RM ($NB_BACKUP_TO_RM 是我要在列表中删除的文件数)之后,这两个命令返回正确的文件。

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).在此之后我想删除所有这些,所以我做了一个xargs rm -- ,但是 Bash 返回文件不存在......我认为这个命令没有运行到远程目录中,而是在本地目录中,而且我不知道我能做些什么来删除这些文件(我的退货清单)。

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 )如何删除我的列表文件(在ls -1tr $REMOTE_DIR/full_backup_ftp*head -$NB_BACKUP_TO_RM

Thanks for your help谢谢你的帮助

Starting SFTP connection can be time consuming.启动 SFTP 连接可能很耗时。 Slightly modified solution to avoid multiple lftp sessions below.稍作修改的解决方案以避免以下多个 lftp 会话。 It will perform much better the the alternative solution, especially if large number of files have to be purged.替代解决方案的性能要好得多,尤其是在必须清除大量文件的情况下。

Basically, leveraging lftp flexibility to mix lftp command with external commands.基本上,利用 lftp 的灵活性将 lftp 命令与外部命令混合。 It creates a command file with a series of 'rm' (leveraging head,xargs, ...), and executing those commands INSIDE the same lftp session.它创建一个带有一系列“rm”的命令文件(利用 head、xargs、...),并在相同的 lftp session 内执行这些命令。

Also note that lftp 'ls' does not allow wildcard, use 'cls' instead另请注意,lftp 'ls' 不允许使用通配符,请改用 'cls'

Make sure you test this carefully, because of potential removal of important files请务必仔细测试,因为可能会删除重要文件

lftp -e $USER,$PASSWORD $HOST <<__CMD__
   cls -1tr $REMOTE_DIR/full_backup_ftp* | head -$NB_BACKUP_TO_RM | xarg -I{} echo rm {} > rm_list.txt
   source rm_list.txt
__CMD__

Or with one liner, using 'lftp' ability to execute dynamically generated command (source -e).或者使用一个班轮,使用“lftp”功能来执行动态生成的命令(source -e)。 It eliminate the temporary file.它消除了临时文件。

lftp -e $USER,$PASSWORD $HOST <<__CMD__
   source -e 'cls -1tr $REMOTE_DIR/full_backup_ftp* | head -$NB_BACKUP_TO_RM | xarg -I{} echo rm {}'
__CMD__

Looks xargs is unknown cmd for lftp after man lftp .看起来 xargs 在man lftp之后对于 lftp 是未知的 cmd 。 And xargs rm is deleting local files not remote files.并且xargs rm正在删除本地文件而不是远程文件。

so please use xargs as below, it works for me.所以请使用下面的 xargs,它对我有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM