如何在 Linux 中使用 bash 压缩 90 天的旧文件并将其移动到特定文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21302162/
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 zip 90days old files and move it to a specific folder using bash in linux
提问by makalshrek
I have lots of files in my FILES folder. I want to zip the files that are 90days old then remove it from the FILES folder and move it to the ARCHIVES folder using bash in linux.
我的 FILES 文件夹中有很多文件。我想压缩 90 天前的文件,然后将其从 FILES 文件夹中删除,然后在 linux 中使用 bash 将其移动到 ARCHIVES 文件夹中。
This is my folder structure:
这是我的文件夹结构:
root@user:/var/FILES
root@user:/var/FILES
root@user:/var/ARCHIVES
root@user:/var/ARCHIVES
I have created a script to zip a file but don't know how to specify the age of the file
我创建了一个脚本来压缩文件,但不知道如何指定文件的年龄
zip -r zipped.zip *.*
so i coded something like
所以我编码了类似的东西
FILE=find *.* -mtime +90
zip -r zipped.zip $FILE
but only returns error. Thanks
但只返回错误。谢谢
采纳答案by anubhava
You can use:
您可以使用:
find . -mtime +90 -exec zip zipped.zip '{}' +
EDITIf you want move zipped file to an archive folder then you can do:
编辑如果要将压缩文件移动到存档文件夹,则可以执行以下操作:
find . -mtime +90 -exec zip zipped.zip '{}' + && mv zipped.zip /var/ARCHIVES
回答by ray
you can try find
你可以试试 find
find /var/FILES/ -type f -mtime +90 -exec zip -r zipped.zip {} \; -exec mv {} /var/ARCHIVES \;
Not sure whether I understand you correct, if you want to save zipped.zip in /var/ARCHIVES just use this:
不确定我的理解是否正确,如果您想将 zipped.zip 保存在 /var/ARCHIVES 中,请使用以下命令:
find /var/FILES/ -type f -mtime +90 -exec zip -r /var/ARCHIVES/zipped.zip {} \;