bash 如何从 Microsoft FTP 服务器中删除超过 N 周的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/307951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete files older than N weeks from a Microsoft FTP server
提问by Gerhard Wessels
I run an OpenSuse server that uploads zipped source code backups to a Microsoft FTP server every night. I have written a Bash script that does this through a cron job.
我运行 OpenSuse 服务器,每天晚上将压缩的源代码备份上传到 Microsoft FTP 服务器。我已经编写了一个 Bash 脚本,它通过 cron 作业来完成此操作。
I want to delete backed up files that are older than a certain date. How could I do this?
我想删除早于某个日期的备份文件。我怎么能这样做?
采纳答案by David Dibben
You can delete files on the FTP server using the delete or mdelete FTP commands. I don't know of a way to select old files as a server-side operation, so one option would be to do an FTP ls to get a list of the files on the server, then parse the output to pick up those files which are older than your specified date. Then delete each one using an FTP command.
您可以使用 delete 或 mdelete FTP 命令删除 FTP 服务器上的文件。我不知道有什么方法可以选择旧文件作为服务器端操作,所以一个选择是执行 FTP ls 以获取服务器上的文件列表,然后解析输出以获取那些文件早于您指定的日期。然后使用 FTP 命令删除每个。
If you have a local copy of all the files then it is probably easier to generate the list of files locally using find then delete them one at a time from the server.
如果您有所有文件的本地副本,那么使用 find 在本地生成文件列表可能更容易,然后从服务器中一次删除一个。
If you have some control over the FTP server then using rysnc instead of FTP would probably be easier.
如果您对 FTP 服务器有一定的控制权,那么使用 rysnc 而不是 FTP 可能会更容易。
回答by Adam Rosenfield
The following deletes all files under the directory tree rooted at dirwhose last modification time was before November 1:
下面删除以dir为根目录树下最后修改时间在11月1日之前的所有文件:
find dir -type f \! -newermt 2008-11-01 -exec rm '{}' \+
The date/time format should be ISO 8601; I don't know if other formats are accepted.
日期/时间格式应为 ISO 8601;我不知道是否接受其他格式。
回答by Luca Gibelli
Unfortunately deleting old files from an FTP server is not as simple as running find . -mtime +30 -delete because usually you don't get shell access to your FTP space. Everything must be done via FTP.
不幸的是,从 FTP 服务器删除旧文件并不像运行 find 那样简单。-mtime +30 -delete 因为通常您无法通过 shell 访问您的 FTP 空间。一切都必须通过 FTP 完成。
Herecomes a simple perl script that does the trick. It requires the Net::FTPmodule.
这里有一个简单的 perl 脚本可以解决这个问题。它需要Net::FTP模块。
回答by Kevin Osborne
/*******************************************************************************************
* Author: Kevin Osborne
* This java app aims to delete non-empty directories from an FTP server that are older than
* 45 days, the 45 can be changed to whatever. I believe it's recursive, but I've only tried
* with 3 levels deep, so I can't guarantee anything beyond that, but it worked for my needs
* and hopefully it will for yours, too.
*
* It uses ftp4j, which I found to be incredibly simple to use, though limited in some ways.
* feel free to use it, I hope it helps. ftp4j can be downloaded as a jar file here:
* http://www.sauronsoftware.it/projects/ftp4j/ just include it in your IDE.
*******************************************************************************************/
package siabackupmanager;
import java.util.Calendar.*;
import java.util.*;
import it.sauronsoftware.ftp4j.*;
public class SIABackupManager {
// @SuppressWarnings("static-access")
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Usage: java -jar SIABackupManager.jar HOST USERNAME PASSWORD");
System.exit(0);
}
try {
FTPClient client = new FTPClient();
String hostname = args[0];
String username = args[1];
String password = args[2];
client.connect(hostname);
client.login(username, password);
FTPFile[] fileArray = client.list();
for (int i = 0; i < fileArray.length; i++) {
FTPFile file = fileArray[i];
if (file.getType() == FTPFile.TYPE_DIRECTORY) {
java.util.Date modifiedDate = file.getModifiedDate();
Date purgeDate = new Date();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -45);
purgeDate = cal.getTime();
if (modifiedDate.before(purgeDate)) {
String dirName = file.getName();
deleteDir(client, dirName);
client.changeDirectoryUp();
client.deleteDirectory(dirName);
}
}
}
} catch(Exception ex) { System.out.println("FTP error: " + ex.getMessage()); }
}
public static void deleteDir(FTPClient client, String dir) {
try {
client.changeDirectory(dir);
FTPFile[] fileArray = client.list();
for (int i = 0; i < fileArray.length; i++) {
FTPFile file = fileArray[i];
if (file.getType() == FTPFile.TYPE_FILE) {
String fileName = file.getName();
client.deleteFile(fileName);
}
}
for (int i = 0; i < fileArray.length; i++) {
FTPFile file = fileArray[i];
if (file.getType() == FTPFile.TYPE_DIRECTORY) {
String dirName = file.getName();
deleteDir(client, dirName);
client.changeDirectoryUp();
String currentDir = client.currentDirectory();
client.deleteDirectory(dirName);
}
}
} catch (Exception ex) { System.out.println("deleteDir error: " + ex.getMessage()); }
}
}

