Is it possible to do a equivalent 'find . -ctime n' (Unix command) in JDK7? i.e. find all files based on last changed time? I had a look at the new FileVisitor/BasicFileAttributes/SimpleFileVisitor classes but I cannot see how it could be done.
4 Answers
The following worked for me (using Files.walkFileTree and a FileVisitor) :
FileTime ctime = (FileTime) Files.getAttribute(path, "unix:ctime");

- 914
- 2
- 13
- 25
In the JDK 7 Forums there is discussion opened on the subject
It basically says:
From creationTime's description "If the file system implementation does not support a time stamp to indicate the time when the file was created then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z)". So the behavior you observe is expected. The time of last status change is available if you really need it, try Files.getAttribute(path, "unix:ctime").
So, your own answer seems to be the right one.

- 76,803
- 25
- 144
- 205
You can get the creation time of a file by calling getCreationTime()
on its file attributes object. You can do a directory tree walk using Files.walkFileTree
and a FileVisitor
. Put these together and you can implement find . -ctime n
.

- 698,415
- 94
- 811
- 1,216
-
Creation time and last changed time are not the same. I've already tried getCreationTime(), lastAccessTime(), and lastModifiedTime(). None of them mimic -ctime. – Wayne May 20 '11 at 22:07
-
1As another example, if I were to sort by getCreationTime(), then this would be the equivalent of 'ls -lt', but what I'm trying to do is a 'ls -ltc' sort. – Wayne May 20 '11 at 22:40
-
After further investigation, I think getCreationTime() is broken. It seems that getCreationTime() and lastModifiedTime() are returning the same values. – Wayne May 21 '11 at 02:50
Here's what creationTime's javadoc says:
If the file system implementation does not support a time stamp to indicate the time when the file was created then this method returns an implementation specific default value, typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z).
As the creation is not typical on Unix/Linux then the method is returning the last modified time.

- 2,807
- 3
- 22
- 6