简体   繁体   English

查找和删除多个目录中的文件

[英]Find and remove files in multiple directories

Right now I have a cron job that runs once a week to find and remove files for me that have accumulated during that time. 现在,我有一份cron作业,每周运行一次,以查找和删除在这段时间内积累的文件。 It goes something like this: 它是这样的:

find $DIR1 -maxdepth 1 \( -name \file1-\* -o -name \*file2-\* \) -mtime +7 >> /tmp/file.txt

However, I want to do the same thing in another directory on the server (must be $DIR1 and $DIR2 - doing a find from root across the whole system isn't viable) but I feel that, while repeating the command would work, it would be ugly, redundant and inefficient. 但是,我想在服务器上的另一个目录中执行相同的操作(必须为$ DIR1和$ DIR2-在整个系统中从根目录进行查找是不可行的),但是我觉得,尽管重复执行该命令,这将是丑陋的,多余的和低效的。

So, I want to build a list somehow (ie FILES="file1 file2 file3") and have a streamlined method of checking both directories and then outputting the results of what's found to the text file like above. 因此,我想以某种方式建立一个列表(即FILES =“ file1 file2 file3”),并具有一种精简的方法来检查两个目录,然后将所发现的结果输出到上述文本文件中。

I'm not certain of how to do this - I've thought of perhaps doing a loop, but that seems too basic. 我不确定如何执行此操作-我想过可能要做一个循环,但这似乎太基本了。 Can anyone throw me a bone here and point me in the right direction? 有人可以在这里扔我一块骨头,并指出正确的方向吗? The big thing I'm worried about is getting this to work with find. 我担心的大事情是如何将它与find一起使用。

Edit: To clarify, the second search in $DIR2 is for files older than 14 days (mtime +14) so just adding $DIR2 into the existing string won't work. 编辑:澄清一下,在$ DIR2中进行的第二次搜索是针对14天以上的文件(mtime +14),因此仅将$ DIR2添加到现有字符串中将不起作用。

Thanks! 谢谢!

您是否尝试过:

find $DIR1 $DIR2 -maxdepth 1 \( -name \file1-\* -o -name \*file2-\* \) -mtime +7 >> /tmp/file.txt

It would be much easier to write a bash script ie delete_files.sh and add the bash script in crontab. 编写bash脚本(即delete_files.sh)并将bash脚本添加到crontab中会容易得多。 So next time, if you want to add any more patterns with different directories then you just have to write in bash script only and it will be added in cronjob automatically. 因此,下一次,如果您想添加更多具有不同目录的模式,则只需要编写bash脚本,它将自动添加到cronjob中。

#!/usr/bin/bash

echo $(find $DIR1 $DIR2 -maxdepth 1 \( -name \file1-\* -o -name \*file2-\* \) -mtime +7) >> /tmp/file.txt;

OR 要么

#!/usr/bin/bash

echo $(find $DIR1 -maxdepth 1 \( -name \file1-\* -o -name \*file2-\* \) -mtime +7) >> /tmp/file.txt;
echo $(find $DIR2 -maxdepth 1 \( -name \file1-\* -o -name \*file2-\* \) -mtime +7) >> /tmp/file.txt;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM