This is quite a unique one.

I'm looking for a way to list all top-level directories in a folder that contain sub-directories with new or modified files less than 30 days old in them.

We use a replication method to sync user files to a central repository and I'm looking for a way to monitor which users have synced files within the last month. We can then troubleshoot the ones that are not.

I've used "find . -type d -mtime -30" but this lists every sub-folder and the output is too big to provide a concise picture.

Any help would be much appreciated.

  • What operating system are you on? Do you have GNU find?
    – terdon
    May 6, 2016 at 8:32

3 Answers 3


I would use find on each directory in turn, and pipe it to grep -q . to check if there are any results, and print the directory name conditionally upon whether or not there were any results:

for d in */; do find "$d" -type f -mtime -30 | grep -q . && printf %s\\n "$d"; done
  • 1
    Thank you both so much for your replies. This worked perfectly for me.
    – Vard0
    May 6, 2016 at 8:24

This might be not efficient:

find -mindepth 3 -maxdepth 3 -type f -mtime -30 | 
    xargs -n 1 dirname | 
    xargs -n 1 dirname | 
    uniq # (or sort -u, I assume find's output is already sorted)
  • 3
    +1. but use -print0 with find, -0 with xargs AND -z with both dirname and uniq/sort -u (requires a recent-ish version of GNU coreutils)....otherwise this will break on any file/dir name containing spaces/newlines/etc. BTW, find's output isn't sorted, so use sort -z -u. Finally, pipe the output through tr '\000' '\n'. Resulting in find -mindepth 3 -maxdepth 3 -type f -mtime -60 -print0 | xargs -0r -n 1 dirname -z | xargs -0r -n 1 dirname -z | sort -z -u | tr '\000' '\n'
    – cas
    May 6, 2016 at 8:38
find */ -type f -mtime -30 -print0 | 
    sed -z -e  's:/.*$::' | 
    sort -z -u | 
    tr '\000' '\n'

Requires GNU sort and GNU sed for the -z (NUL-separated input) options.

find lists all files in subdirectories of the current directory less than 30 days old. output is piped into sed to remove everything after the first /, then into sort to unique sort the resulting directory names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.