I want to delete directories only if they have not changed in the last N days. And by changed I mean contain any files that are more recent than N.
I constructed something similar / found this but it doesn't do exactly what i was looking for.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
I would loop over the directories in question (here, every subdirectory of the current directory) and count the number of lines that
find outputs; if that number is zero, then remove the directory. The
find command simply asks for files in the given directory that are less than "N" days old (modified in the past "N" days).
N=3 for dir in */ do count=$(find "$dir" -type f -mtime -"$N" -print | wc -l) [ "$count" -eq 0 ] && echo rm -r -- "$dir" done
echo if the results look correct.
My idea is like this:
touch -d "4 days ago" .reference_file for dir in */ do [ $dir -ot .reference_file ] && rm -rf "$dir" done
First, we create a file .reference_file, with create date set as "4 days ago".
Then, for all directories in this working dir we check that its modification time is older (
-ot operator) than modification time of .reference_file.
If is - we remove that directory by
rm -rf $dir
Each time we run this script, modification date for .reference_file will be updated to be always 4 days before now.