Find directories with all files inside older than X?

Is it possible on linux to find directories where all contained files and directories (and sub-directories' files etc.) are older than a given age? In other words, if a directory has one or more files within that have a modification date more recent than a given threshold, that directory should not be listed. If all of the files and folders below that directory are older than the given threshold, then that directory should be listed.

The use case is that my home directory is full of hidden directories, and I'm sure that many of them are left overs from previous installations, and software that I haven't used in years. I'd like to be able to find these directories, so I can easily decide whether to cull them.


It's probably possible to do this without creating files using process substitution or something, but here's a quick-and-dirty solution:

find . -type f -mtime +30 -printf '%h\n' | sort | uniq > old.txt
find . -type f -mtime -30 -printf '%h\n' | sort | uniq > new.txt
grep -vf new.txt old.txt

The first command outputs the path of every file modified more than 30 days ago (in find's -printf -- at least with the GNU find on my system -- %h prints the whole path except for the actual filename), then sorts those and gets rid of any duplicates, and puts the whole thing into a file called old.txt.

The second command does the same but with every file modified less than 30 days ago, and puts them into another file, new.txt.

The grep line prints every line from old.txt that doesn't appear in new.txt -- so it will give you a list of directories that contain only files that were last modified more than 30 days ago.

This is all using the GNU versions of the utilities. I don't know if the syntax matches up on the BSD versions, etc.


Finally figured out the magic one-liner:

for dir in `find . -type d -mtime +30`; do test `find $dir -type f -mtime -30 -print -quit` || echo $dir; done

This prints any directories that have a modification time greater than 30 days and no files modified within the last 30 days.


This one should work:

for DIR in {the folder list}; do
   if [[ $(find ${DIR} -mtime -10 -type f) == "" ]]; then 
        echo ${DIR}
   fi
done

It finds all directories in {the folder list}, that contains files older than 10 days.

Basically what find ${DIR} -mtime -10 -type f does is to find all files (-type f) that are modified within 10 days (-mtime -10, which is different from -atime or -ctime, see here for more information: http://www.cyberciti.biz/faq/howto-finding-files-by-date/). If such files are not found within a directory, then we know that this directory is older than 10 days, and we output this directory with echo ${DIR}.

Hope it helps!