Take the 2-minute tour ×

I am working on converting a KornShell (ksh) script to Groovy. I have the following Find command - what would be a Groovy way to do something similar, without relying on Unix commands (I need this to work cross-platform, so I can not do a "blah blah".execute()).

find <source directory> -name <file pattern> -type f -mtime +140 -level 0

This code searches for all files in the source directory (no subdirs) that match a file pattern and are older than 140 days.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Groovy provides some methods for searching through directories: File.eachFile for the -level 0 case, or File.eachFileRecurse for the general case. Example:

use(groovy.time.TimeCategory) {
    new File(".").eachFile { file ->
        if (file.isFile() &&
            file.lastModified() < (new Date() - 140.days).time) {
            println file
share|improve this answer
or if you're doing level 0, you could do File.listFiles() and follow that with findAll to just get the old files into a collection –  tim_yates Dec 21 '12 at 20:49
The asker also wanted to match name against a pattern which needs to use eachFileMatch() such as new File('.').eachFileMatch(~/.*?\.JPG/) {... –  JasonM1 Dec 22 '12 at 0:45

Your Answer


