wood burning stoves 2.0
The moose likes Linux / UNIX and the fly likes Finding the modified date time of a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Finding the modified date time of a file" Watch "Finding the modified date time of a file" New topic

Finding the modified date time of a file

renu richard
Ranch Hand

Joined: Oct 06, 2008
Posts: 116

I want to find the last modified time of a file and find the difference between the currrent time and the last modified time. Appreciate, if someone can throw some light on what commands can be used.

Tom Reilly

Joined: Jun 01, 2010
Posts: 618
You posted in the Linux/UNIX forum. Do you mean you want to do this in a Java application?
renu richard
Ranch Hand

Joined: Oct 06, 2008
Posts: 116
No..I want to do this in Unix/Linux. Looking for unix commands.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16395

The "ls -l" command will list modified times. Linux has extensions that allow a more useful formatting of that time, specifically the "--time-style' option. It uses the same date/time formatting string syntax that the "date" command does.

So you can get the difference between file time and current time by using the "ls" and "time" commands to output simple numeric values and subtracting them. I'm not sure about how to format that interval in something like "days, months, weeks" or whatever.

Of course, for many problems, this is doing things the hard way. If you just want to archive old files that haven't been used in 6 months (180 days), you can do things like this:

zip -r -m unused-files.zip `find -atime +180`

You can use this with "tar" as well, although I don't think tar has an equivalent to the zip "-m" (move) option. Tar has a built-in date threshold test, but I think it only works on modification times, which doesn't help for finding unused read-only files.

Customer surveys are for companies who didn't pay proper attention to begin with.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

This will produce a list of filenames + age in seconds * -1:

It is sorta covered in the JavaRanch Style Guide.
subject: Finding the modified date time of a file