[英]How to delete the zip files except last few, in multiple folder hierarchy
Folder structure goes like this: 文件夹结构如下所示:
Main_Folder:
A
--> File1.zip
--> File2.zip
A11
--> File3.zip
--> File4.zip
A12
--> File5.zip
--> File6.zip
B
C
Similary, this remains same for B, B11 , B12 & C, C11 , C12 respectively. 相似地,这对于B,B11,B12和C,C11,C12仍然相同。 I want to execute the command from Main_Folder path, such that it traverses into rest all inner folders and deletes all zip files except last few zip files.
我想从Main_Folder路径执行命令,以便它遍历其余所有内部文件夹并删除除最后几个zip文件以外的所有zip文件。 Could you please help me out to achieve this ?
你能帮我实现这个目标吗?
I tried 我试过了
find $PWD -printf "%TY-%Tm-%Td %TT %p\n" | sort -n | grep "zip" | head -n -5| xargs rm -rf
But this keeps latest files overall, not folder by folder. 但这会保持最新文件的整体状态,而不是逐个文件夹。
You usually choose some time that you want times kept. 您通常会选择一些想要保留的时间。
# DANGEROUS!!!
$ find PATH -mtime +2 -name "*.zip" -delete
But you wont be sure that there are enough files kept. 但是您将不确定是否保留了足够的文件。 eg if your backup stopped 2 days ago, you may remove your last backups.
例如,如果您的备份在2天前停止,则可以删除最后的备份。
For it is good to first check you have enough backups. 因为最好先检查一下是否有足够的备份。 eg
例如
existing=`find PATH -mtime -2 | wc -l`
if (( existing > 5 )); then
find ... -delete
fi
Please test code very carefully before running. 在运行之前,请非常仔细地测试代码。 I did not check syntax and calculations of the snippets above.
我没有检查上面片段的语法和计算。 Commands a re dangerous to run.
命令运行非常危险。 Use common sense.
使用常识。
HTH 高温超导
Now I see you want to remove folders. 现在,我看到您要删除文件夹。 Then you can search for
-mtime ... -type d
and use above logic. 然后,您可以搜索
-mtime ... -type d
并使用上述逻辑。 But insted of -delete
you'd use something like -exec rm -rf \\{\\} \\;
但是,在安装
-delete
您将使用-exec rm -rf \\{\\} \\;
类的东西-exec rm -rf \\{\\} \\;
which is even more dangerous. 这更加危险。
Or you can get a list of folders and then delete zip files over them. 或者,您可以获取文件夹列表,然后删除其上的zip文件。 It really depends on specific use case.
这实际上取决于特定的用例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.