9

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.

2
  • 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, 2012 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, 2012 at 1:13

5 Answers 5

7

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.

2
7

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.

1
  • Nice. That is very cool.
    – naught101
    Feb 10, 2016 at 23:37
1

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!

2
  • 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. Jul 20, 2016 at 8:59
  • @MariusMatutiae Thanks for your advice, I've updated the answer. Jul 20, 2016 at 14:00
0
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

1
  • 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, 2012 at 6:09
0

You could use the following workaround to implement your need:

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

At touch man page:

-t STAMP
    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

3
  • 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, 2012 at 0:56
  • you could use -d option on touch command which will enable you to create file at a given date. Jun 27, 2012 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, 2012 at 1:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .