简体   繁体   中英

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. 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.

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.

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.

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.

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. 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.

#!/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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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