Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
What do you mean by "older": modification or access time? Note that the latter will be meaningless if the file system is mounted with relatime or noatime. Also a very old modification time doesn't necessarily mean that a file is useless. – cYrus Jun 26 '12 at 7:39
@cyrus modification time. I know it doesn't mean it's useless. As I said, I just want to use this as a tool to help me to decide what is useless.. – naught101 Jun 28 '12 at 1:13
up vote 3 down vote accepted

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.

share|improve this answer
Nice. That's a neat solution! – naught101 Oct 7 '13 at 23:21
grep can be slow and memory intensive if the directory listing is huge. In this case you could use comm instead. Refer… – shonky linux user Mar 6 '14 at 6:03

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.

share|improve this answer
Nice. That is very cool. – naught101 Feb 10 at 23:37
find -type f -mtime +30

to check files older than 30 days

if you add -iname '.*' it will show you the hidden files

or -type d for directories

share|improve this answer
Yes, I know this. I'm not looking for files older than the date, I'm looking for directories which only contain files and directories which are all older than the given date. – naught101 Jun 26 '12 at 6:09

You could use the following workaround to implement your need:

$ touch -t 201206010100 some_file
$ find . -newer some_file

At touch man page:

    use [[CC]YY]MMDDhhmm[.ss] instead of current time

There is also -d option of touch but i haven't used it. You could give it a try.

[Edit] you could use touch -d option as follows to generate a file at a given date:

$ touch -d "Wed Jun 27 09:44:44 EEST 2012" some_file
$ ls -l some_file
-rw-r--r-- 1 user user 0 2012-06-27 09:44 some_file

The you could use the same approach i mentioned above

share|improve this answer
See my comment on Petkaux answer. This doesn't solve my question, and it's also a bit nutty, Why wouldn't I just use find -newert 201206010100 ? – naught101 Jun 27 '12 at 0:56
you could use -d option on touch command which will enable you to create file at a given date. – scriptmonster Jun 27 '12 at 6:44
Before you spend more time on this answer, I think you should re-read the question. It is not asking what you think it is asking. – naught101 Jun 28 '12 at 1:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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