Identify age of the file.

Hi all,
I'm using SunOS.
need to find age of the file in terms of seconds. The file
name with its path will be given to the script as input.

     Any kinda help will be appreciated.

    Thanks in advance

You can get the mtime of a file as unixtime with GNU ls like:

$ /usr/local/bin/ls -l --time-style=+%s /tmp
total 8
srwxrwxrwx 1 mysql mysql   0 1274881760 mysql.sock
-rw-r--r-- 1 root  root   30 1274881759 ogl_select3820
-rw------- 1 mysql mysql 849 1274881759 sh39510

You can get the current unixtime with perl:

$ perl -e "print time"
1275980113

The rest is to parse these outputs to find the age of a file in seconds.

GNU ls for Solaris is available from sunfreeware.com in the coreutils package.

thanx hergp,
But i cannot use any tool. i jus need to write a pure script. I'm very new to unix, and i've been told that there are inbuilt functions in Solaris to find Timestamp and then using that i can find out how old is the file.

Solaris ls comes with the -e option which prints the mtime up to the second, but in the format "mmm dd hh:mm:ss yyyy " which makes it harder to parse.

You need to write a shell function or shell script which takes two timestamps, one being the timestamp from ls and one the output of the date command, and then calculate the time difference.

1 Like

hi hergp,
I tried browsing man for ls -e, It says no entry for -e...
I understood the logic you gave and will be trying the same, is it possible to extract timestamp of a file in some other fashion..??

The -e switch was not available before Solaris 10.

I don't think there is another Solaris standard command that is capable to give you a files mtime to the second.

If the situation allows perl progamming, then this is probably the best way to go with Solaris preinstalled software to solve this problem.

$ perl -e "print time"
1275980113

I tried using the above code. And it gives me an error:

No code specified for -e.

Can some one help me with this..??

Did you copy and paste the text to a terminal window? Maybe the quote signs got messed up.

Please try to type the text into your terminal window, so that correct double-quote signs are used, not typographic characters.