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.

| 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

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.

| improve this answer | | | | |

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.

| improve this answer | | | | |
  • Nice. That is very cool. – naught101 Feb 10 '16 at 23:37

This one should work:

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

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!

| improve this answer | | | | |
  • When you suggest a solution to a problem, it is most helpful, also for future readers, to explain in some detail why your solution works. The key point here is the use of the -mtime option of find: which units does it use (minutes,hours,days), how does it differ from atime, thin kind of things. – MariusMatutiae Jul 20 '16 at 8:59
  • @MariusMatutiae Thanks for your advice, I've updated the answer. – username123 Jul 20 '16 at 14:00
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

| 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

| 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. – İsmail Taha AYKAÇ 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 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.