Reputation: 31
I have a script which deletes files older than +2 days in a specific Directory. I would like to check if there is a file with todays date created before removing the older files. This is what I have:
#!/bin/bash
find /var/backups/server1 -type f -mtime +2 -exec rm {} \;
find /var/backups/server2 -type f -mtime +2 -exec rm {} \;
find /var/backups/server3 -type f -mtime +2 -exec rm {} \;
find /var/backups/server4 -type f -mtime +2 -exec rm {} \;
find /var/backups/server5 -type f -mtime +2 -exec rm {} \;
So Basically: 1.Check Directory with file with todays date. 2.If affirmative find /var/backups/serverX -type f -mtime +2 -exec rm {} \; 3.If not "execute scriptX" (which maybe a mail notification)
thanks!
Upvotes: 1
Views: 201
Reputation: 21955
You could do something like this
find /var/backups/ -maxdepth 1 -type d -print0 | while read -rd '' dirname
do
arry=( $(find "${dirname}" -type f -atime 0) )
#Checks if there is a file that is updated today.
[ "${#arry[@]}" -ge 1 ] && find "${dirname}" -type f -mtime +2 -exec rm {} \;
done
Upvotes: 1