File creation time in seconds

Hi All,

Cany any one help me in solving this..
Problem statement: I have a requirement to find the time from which there are no files created in a given directory. For this I am assuming that I need to get the file creation time in seconds, then the current time in seconds using `date +%s`. Then calculating the time by subtracting file creation time from date.

Please, help me in getting the file creation time in seconds..

Thanks in advance..

Hi.

If a file is created in a directory, then the timestamp of the directory is updated.

You can get the timestamp of that (seconds since epoch) using the perl stat function, something like:

perl -e '$epoch = (stat("."))[9]; print "$epoch";'

(. is the current directory)

Subtracting that from date '+%s' would give the difference in seconds.

Hi,

I am new to Perl scripting can you suggest me if there is an option using unix shell scripting ..
or can you please help me with an example script

scottn's code is fine for what you want

You may want to revisit your assumption: for performance reasons, file metadata is not always updated every time an open file is written to. Depending upon a lot of factors the directory could be stale and your test would think no file writes had occurred, when in fact they have.

See the attached white paper.

Thank You for helping me..

It always smells of homework when someone, when given a "solution" that is perfectly simple asks to have it by a specific means.

I'm not familiar with the controls of an Airbus A340, but I still use one to fly to where I want to get to!

filesec()
{
   perl -e '$epoch = (stat("$ARGV[0]"))[9]; print "$epoch";'  "$1"

}

find /somepath -type d | 
while read fname 
do
     now=$(date "+%s" )
     filetime=$( filesec "$fname")
     echo "Directory $fname last updated $(( $now - $filetime ))"
done

-- this code won't work on Solaris, Solaris date does not like %s

scottn - It may be homework, but I kinda doubt it. We get this question a lot.

The systems that I've used have a stat utility that wraps the stat syscall. If you have that, you can use it instead of perl (not that there's anything at all wrong with perl ... respect heheh ;)).

Regards,
Alister

---------- Post updated at 09:17 PM ---------- Previous update was at 09:11 PM ----------

That prompted me to take a look at the posix date man page. I'm more than a little stunned that epoch isn't a standardized format specifier. Not even a mention in the rationale. Weird.

Man Page for date (POSIX Section 1) - The UNIX and Linux Forums