I have been looking for a way to archive our fileserver for some time now but have not found a working solution. The objective is to search through a directory and it's subdirectories for files not modified in the last x days. When there are no files found in the parent or its subdirectories, the entire parent directory should be archived (or at least printed to screen). Any suggestions are appreciated! Greetings, John
A simple answer is to use the find command's "newer" argument, which finds any files newer than another file. Because what we want is actually the inverse, we'll need a script that counts the files that have been modified (i.e., are newer) and prints the name of the directory if NONE are found.
DATE=`date -d "-30 days"` #Calculate a date 30 days before today touch -d "$DATE" /tmp/newer #Create a temp file with the calculated date #Assume we're passing in a list of directories #We could just as easily find any directory below a certain point without #newer files by setting DIR=`find /what/ever/directory -type d` for DIR in $* do MOD=`find "$DIR" -newer /tmp/newer | wc -l` #Count the number of newer files if [ "$MOD" -eq 0 ] then echo $DIR # Nothing new in 30 days - Print the name of the Dir fi done