find . -newermt "2020-03-31 00:00:00" -not -newermt "2020-04-31 00:00:00" -exec rm -f {}\;
Java代码
public void deletePic(String start,String end) throws IOException {
String deleteString = "find {0} -name \"*jpg\" -newermt \"{1}\" -not -newermt \"{2}\" -exec rm -f {3} \\;";
deleteString = MessageFormat.format(deleteString,CommonConstants.PIC_SNAP,start,end,"{}");
String[] cmd = new String[]{"/bin/sh","-c",deleteString};
Runtime.getRuntime().exec(cmd);
}
注意
命令的拼接一定要用MessageFormat.format(String pattern, Object ... arguments)
进行参数的传递,不然生成的命令串虽然正确但是Runtime.getRuntime().exec()
执行并不生效
也可以把 -newermt 换为下列参数:实现删除前n天的文件
-amin -n 查找系统中最后N+-分钟访问的文件
-atime -n 查找系统中最后n*24小时访问的文件
-cmin -n 查找系统中最后N分钟被改变文件状态的文件
-ctime -n 查找系统中最后n*24小时被改变文件状态的文件
-mmin -n 查找系统中最后N分钟被改变文件数据的文件
-mtime -n 查找系统中最后n*24小时被改变文件数据的文件
-amin +n 查找系统中N+-分钟之前访问的文件
-atime +n 查找系统中n*24小时之前访问的文件
-cmin +n 查找系统中N分钟之前被改变文件状态的文件
-ctime +n 查找系统中n*24小时之前被改变文件状态的文件
-mmin +n 查找系统中N分钟之前被改变文件数据的文件
-mtime +n 查找系统中n*24小时之前被改变文件数据的文件