简体   繁体   English

如何在多个文件夹层次结构中删除除最后几个文件以外的zip文件

[英]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.

相关问题 如何删除除 linux bash 中按日期排序的最后 5 个文件以外的所有文件 - How to delete all files except last 5 ordered by date in linux bash 如何删除目录中除一个文件夹和一个文件以外的所有文件? - how to delete all files in directory except one folder and one file? 除了特定的其他文件夹之外,如何使用 zip ssh 命令压缩文件夹? - How to zip a folder with zip ssh command except specific other folder? 使用 bash 命令删除除少数文件之外的所有文件 - Delete all files except a few using bash command 如何在linux中压缩其中一个后立即批量删除同一文件夹中的mp4文件? - How to batch delete mp4 files in a same folder right after zip one of them in linux? Linux命令删除除.git文件夹以外的所有文件? - Linux command to delete all files except .git folder? 如何在基于 4GB 大小限制的文件夹中创建多个 ZIP 文件? - How to create multiple ZIP files in a folder based on 4GB size restriction? 我想通过 shell 删除所有文件,除了少数文件,但会导致语法错误:“(”意外 - i want to delete all files via shell except few as but results in Syntax error: "(" unexpected 删除linux目录中的所有文件夹和文件,除了一个文件夹和该文件夹中的所有内容 - delete all folders and files within a linux directory except one folder and all contents inside that folder 如何使用zip -T检查多个zip文件的CRC - How can I check CRC of multiple zip files with zip -T
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM