Operating System - HP-UX
1753295 Members
6490 Online
108792 Solutions
New Discussion юеВ

Get the timestamp of a file on HP?

 
SOLVED
Go to solution
Michelle_61
Frequent Advisor

Get the timestamp of a file on HP?

Hi All,

I want to write a shell script to get the modification timestamp of a file on HP. I cannot use C functions or Perl. The 'ls -l' command is not what I need. What I need is something like below:

START=$(date +%s)

But the 'date' has to be the modification time of a specific file.

Your response will be very helpful to me!
12 REPLIES 12
James R. Ferguson
Acclaimed Contributor
Solution

Re: Get the timestamp of a file on HP?

Hi Michelle:

> I want to write a shell script to get the modification timestamp of a file on HP. I cannot use C functions or Perl.

Why oh why? You really want the 'stat()' function (which LINUX offers as a shell command but HP-UX doesn't).

# perl -le 'for (@ARGV) {print join " ",(stat($_))[9],$_}' file1 file2 ...

If you need to *compare* file modification timestamps you can use the Posix shell's: '-nt' and '-ot' file test operators for comparing newer ('-nt') and older ('-ot') as:

# [ ${file1} -nt ${file2} ] && echo "one newer than two"

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Get the timestamp of a file on HP?

Hi (again) Michelle:

OK, I know of yet another potential solution for you. Look Dennis' response in:

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1320053

The salient code is thus:

# dumpmsg /usr/lib/nls/msg/C/ls.cat > /tmp/ls.msg

# vi /tmp/ls.msg
[ ...and replace lines 11 and 12 with:
%s
]

# gencat /tmp/ls.cat /tmp/ls.msg

# NLSPATH=%N.cat ls -l file

Regards!

...JRF...
Michelle_61
Frequent Advisor

Re: Get the timestamp of a file on HP?

I need to get the exact timestamp of one specific file. The "perl -le 'for (@ARGV) {print join " ",(stat($_))[9],$_}' file" command works perfect. Thanks!

Is the 'perl' command available to the base HP installation, just like the 'ls' command? I need to ensure my script works on all HP systems.
James R. Ferguson
Acclaimed Contributor

Re: Get the timestamp of a file on HP?

Hi (again) Michelle:

Is the 'perl' command available to the base HP installation, just like the 'ls' command? I need to ensure my script works on all HP systems.

Yes it is.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Get the timestamp of a file on HP?

>JRF: I know of yet another potential solution for you. Look Dennis' response in:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1320053

Actually Michelle wants the format where it is less than 6 months. (Or possibly HH:MM:SS.)

So what I had there was fine.

I'll ask that Michelle's duplicate be removed:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1447272
VK2COT
Honored Contributor

Re: Get the timestamp of a file on HP?

Hello Michelle,

You already got good advice and solutions.

Please, please do not rely on timestamps in Unix AT ALL. I am not sure what your goal
is, but timestamps are easy to forge.

Let me demonstrate (this is actually part
of my presentation I did when I was teaching
HP-UX junior support team at a large hospital
recently):

# ls -l /etc/motd
-rw------- 1 root sys 842 Jul 16 2008 /etc/motd

# NLSPATH=%N.cat ls -l /etc/motd
-rw------- 1 root sys 842 1216172154 /etc/motd

# perl -le 'for (@ARGV) {print join " ",(stat($_))[9],$_}' /etc/motd
1216172154 /etc/motd

Now, create a new file:

# touch -r /etc/motd /tmp/BLAH

... and its timestamps the timestamps:

# ls -l /tmp/BLAH
-rw------- 1 root sys 0 Jul 16 2008 /tmp/BLAH

# NLSPATH=%N.cat ls -l /tmp/BLAH
-rw------- 1 root sys 0 1216172154 /tmp/BLAH

# perl -le 'for (@ARGV) {print join " ",(stat($_))[9],$_}' /tmp/BLAH
1216172154 /tmp/BLAH

Good luck and if you are doing these checks
for security audits, you need tools
like Tripwire, AIDE, and so on.

Cheers,

VK2COT


VK2COT - Dusan Baljevic
Hakki Aydin Ucar
Honored Contributor

Re: Get the timestamp of a file on HP?

>VK2COT:
# NLSPATH=%N.cat ls -l /etc/motd
-rw------- 1 root sys 842 1216172154 /etc/motd

Are you sure this command out ? in my environment I got this:

# ls -l /etc/motd
-rw-rw-rw- 1 root sys 210 Oct 27 2009 /etc/motd
# NLSPATH=%N.cat ls -l /etc/motd
-rw-rw-rw- 1 root sys 210 Oct 27 2009 /etc/motd
James R. Ferguson
Acclaimed Contributor

Re: Get the timestamp of a file on HP?

Hi (again):

> Hakki: Are you sure this command out ?

Re-read my comments. You need to create a modified message catalog.

Regards!

...JRF...
Hakki Aydin Ucar
Honored Contributor

Re: Get the timestamp of a file on HP?

>JRF:
Sorry I missed that, thank you for warning.

So, What do you say VK2COT 's infomation about time_stamp unreliability ?
I am totally surprised.