• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding the modified date time of a file

 
renu richard
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You posted in the Linux/UNIX forum. Do you mean you want to do this in a Java application?
 
renu richard
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No..I want to do this in Unix/Linux. Looking for unix commands.
 
Tim Holloway
Saloon Keeper
Posts: 18302
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will produce a list of filenames + age in seconds * -1:


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic