Operating System - Linux
1828221 Members
2130 Online
109975 Solutions
New Discussion

File creation/modification time

 
SOLVED
Go to solution
Nitin Kumar Gupta
Trusted Contributor

File creation/modification time

I want to check a file name, whether it is created/modified within last x mins or not.

How to do it?

I must take into consideration about leap year, year changed etc.

Rgds
-NKG-
3 REPLIES 3
Pete Randall
Outstanding Contributor

Re: File creation/modification time

Use "touch" to create a reference file with the appropriate date/time value, then use the find command with the "-newer" option to compare if the files are more recent than your reference file.

Do a man on touch and a man on find for specifics.


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: File creation/modification time

Hi:

There is no such thing in UNIX as a "creation" timestamp. The last modification ('mtime') is coincidently a creation at the instance that a file is first created. Thereafter it is updated any time the file contents or offset change.

That said, you can use 'find' to look for files meeting your criteria. The '-mtime' argument is a 24-hour increment. The '-newer' argument used with a "reference file" and 'touch' allows measurement from a preset point.

Perl provides the easy way to compute a file age in units of seconds:

# perl -le 'print "OK" if -e $ARGV[0] && -M $ARGV[0] > (3600/86400)' file

...would print "OK" if the file (name) passed as an argument to the Perl script were older than 3600/86400 of a day or older than 1-hour. Granularity is in seconds so one-second would be 1/86400 of a day.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor
Solution

Re: File creation/modification time

Standard smarty pants line: Unix does not maintain a 'created time' for files.


Many ways to solve this
touch + find ... -newer is always handy.

I use perl for just about everything.
It has a -M file function returning the Modified age for a file in days

In a verbose example:

$ perl -le '$file = shift; $_ = ( 5/(24*60) < -M $file) ? "old" : "new"; print'