How to obtain the age of a file (epoch time stamp)

How to obtain the age of a file (epoch time stamp)

Post by kwen » Fri, 30 Jul 2004 19:07:41



Hi all,

I was wondering what it the shortest way out to obtain the age of a
file from inside a shell script, without having to calculate from an
'ls -l' with the current time on the system.

Thanks alot!
weng

 
 
 

How to obtain the age of a file (epoch time stamp)

Post by Stephane CHAZELA » Fri, 30 Jul 2004 19:33:29


2004-07-29, 03:07(-07), kweng:
Quote:> I was wondering what it the shortest way out to obtain the age of a
> file from inside a shell script, without having to calculate from an
> 'ls -l' with the current time on the system.

[...]

Does this work for you?

file=/path/to/the/file
cpio -o 2> /dev/null << EOF | od -x | sed -n 'n
  s/[^ ]* *\(....\) *\(....\).*/16i\1\2p/;y/abcdef/ABCDEF/;p;q'|dc
$file
EOF

(assuming $file does not contain newline characters).
(to get current time, you can use awk srand() function).

If not, you can use GNU find, GNU stat, perl, ruby, python or zsh.

--
Stephane

 
 
 

How to obtain the age of a file (epoch time stamp)

Post by Dana Fren » Sat, 31 Jul 2004 00:52:01



> 2004-07-29, 03:07(-07), kweng:
> > I was wondering what it the shortest way out to obtain the age of a
> > file from inside a shell script, without having to calculate from an
> > 'ls -l' with the current time on the system.
> [...]

> Does this work for you?

> file=/path/to/the/file
> cpio -o 2> /dev/null << EOF | od -x | sed -n 'n
>   s/[^ ]* *\(....\) *\(....\).*/16i\1\2p/;y/abcdef/ABCDEF/;p;q'|dc
> $file
> EOF

> (assuming $file does not contain newline characters).
> (to get current time, you can use awk srand() function).

> If not, you can use GNU find, GNU stat, perl, ruby, python or zsh.

Cooool ...  You get the command line of the week award! ...

In fact I think I'll start a weekly "King of the One Liners" page on
my web site and start saving these things ...

It will be Located under:
  http://www.mtxia.com/fancyIndex/Tools/Scripts

--------------------------------------------------------

Mt Xia Technical Consulting Group   http://www.mtxia.com
100% Spam Free Email              http://www.ridmail.com
MicroEmacs                      http://uemacs.tripod.com
Korn Shell Web     http://dfrench.tripod.com/kshweb.html

 
 
 

How to obtain the age of a file (epoch time stamp)

Post by Stephane CHAZELA » Sat, 31 Jul 2004 01:16:04


2004-07-29, 10:33(+00), Stephane CHAZELAS:
[...]

Quote:> If not, you can use GNU find, GNU stat, perl, ruby, python or zsh.

Or GNU date provided your strftime knows "%s" as the number of
seconds since epoch:

date -r /some/file +%s

--
Stephane

 
 
 

How to obtain the age of a file (epoch time stamp)

Post by Stephane CHAZELA » Sat, 31 Jul 2004 01:18:49


2004-07-29, 08:52(-07), Dana French:
[...]
Quote:>> file=/path/to/the/file
>> cpio -o 2> /dev/null << EOF | od -x | sed -n 'n
>>   s/[^ ]* *\(....\) *\(....\).*/16i\1\2p/;y/abcdef/ABCDEF/;p;q'|dc
>> $file
>> EOF
[...]
> Cooool ...  You get the command line of the week award! ...

[...]

Thanks.

BTW, I posted that one several times and asked each time wether
it was portable or not but got no answer. I've tested it on
Linux, FreeBSD and Solaris, though.

Does anybody have other experiences?

--
Stephane

 
 
 

How to obtain the age of a file (epoch time stamp)

Post by kwen » Sat, 31 Jul 2004 15:47:11


Thanks! Works like a charm. :-)

> 2004-07-29, 03:07(-07), kweng:
> > I was wondering what it the shortest way out to obtain the age of a
> > file from inside a shell script, without having to calculate from an
> > 'ls -l' with the current time on the system.
> [...]

> Does this work for you?

> file=/path/to/the/file
> cpio -o 2> /dev/null << EOF | od -x | sed -n 'n
>   s/[^ ]* *\(....\) *\(....\).*/16i\1\2p/;y/abcdef/ABCDEF/;p;q'|dc
> $file
> EOF

> (assuming $file does not contain newline characters).
> (to get current time, you can use awk srand() function).

> If not, you can use GNU find, GNU stat, perl, ruby, python or zsh.

 
 
 

How to obtain the age of a file (epoch time stamp)

Post by kwen » Tue, 03 Aug 2004 16:08:09


Hi Stephane,

Thanks for the reply on this. But, I'd like to know exactly how the
one line works? How does a cpio+od+sed+dc command produce a time stamp
output?

Thanks in advance,
kweng


> 2004-07-29, 03:07(-07), kweng:
> > I was wondering what it the shortest way out to obtain the age of a
> > file from inside a shell script, without having to calculate from an
> > 'ls -l' with the current time on the system.
> [...]

> Does this work for you?

> file=/path/to/the/file
> cpio -o 2> /dev/null << EOF | od -x | sed -n 'n
>   s/[^ ]* *\(....\) *\(....\).*/16i\1\2p/;y/abcdef/ABCDEF/;p;q'|dc
> $file
> EOF

> (assuming $file does not contain newline characters).
> (to get current time, you can use awk srand() function).

> If not, you can use GNU find, GNU stat, perl, ruby, python or zsh.

 
 
 

How to obtain the age of a file (epoch time stamp)

Post by Stephane CHAZELA » Tue, 03 Aug 2004 16:50:57


2004-08-2, 00:08(-07), kweng:

Quote:> Hi Stephane,

Hi kweng, please don't top-post.

[^^^]

Quote:>> file=/path/to/the/file
>> cpio -o 2> /dev/null << EOF | od -x | sed -n 'n
>>   s/[^ ]* *\(....\) *\(....\).*/16i\1\2p/;y/abcdef/ABCDEF/;p;q'|dc
>> $file
>> EOF

[^^^]

Quote:> Thanks for the reply on this. But, I'd like to know exactly how the
> one line works? How does a cpio+od+sed+dc command produce a time stamp
> output?

[...]

cpio stores the timestamps for the files it archives so that
they can be restored when unarchive. For the first file in the
archive, it happens to be stored in the 17th to 20th bytes in
cpio output.

$ echo / | cpio -o 2> /dev/null | od -x
0000000 71c7 4f8a 5531 41ff 50f9 2931 0009 4f8a
0000020 40f2 2a9f 0002 0000 0000 002f 71c7 0000
[...]

"40f2 2a9f" is the time stamp.

The sed part converts it to
16i40F22A9Fp
Which is a dc script that converts 40f22A9F to 1089612447.

Note that you can't use

od -vAn -j16 -N4 -tu4

as it's less portable, and won't work on little-endian systems.

Note that depending on the system endianness:

printf '\01\02\03\04' | od -x

will give 0201 0403 or 0102 0304.

But the cpio output will also be different on those two kinds of
systems (I guess the endian-ness is stored in the cpio archive
format so that a big-endian machine is able to unarchive a
little-endian made archive)

Little endian for a file 2004-1-1 0:0:0 GMT+1

$ echo a | cpio -o | od -vtx1 | head -2
2 blocks
0000000 c7 71 78 f2 d7 67 a4 81 f9 50 31 29 01 00 78 f2
0000020 f3 3f 00 63 02 00 00 00 3a 03 61 00 4e 65 77 73

Big-endian:
$ echo a | cpio -o | od -vtx1 | head -2
2 blocks
0000000 71 c7 00 02 bd bc 81 a4 00 00 00 01 00 01 ff ff
0000020 3f f3 63 00 00 02 00 00 02 2e 61 00 23 21 20 2f

So, in the end, the "od -x" output is the same (3ff3 6300).
But it may not work on every system, with every cpio
implementaion, that's why I was asking people to test it on
their system.

--
Stephane

 
 
 

1. Time discrepancy between Linux and DOS time stamps for same file

When I look at the time stamp on a dos file using ls in Linux, it reports
a different time than DOS reports for the same file using dir.

This is for the /dosc partition mounted under Linux as a type msdos partition.

The time differential seems to be about 10 1/2 hours; where Linux thinks the
file is 10 1/2 hours older than the DOS time.

Does anyone know a fix or reason for this?

Thanks,


--

2. Defining file that starts at offset of another

3. Epoch time - Date Time in DDMMYYHHMISS ( From-To conversion ) using standard utility

4. Upgrading to XFree86 3.3.3 on RedHat 5.1

5. epoch time <-> human time

6. new XFree86

7. File Time Stamp

8. Space Station, Windows & Unix

9. Changing File Modified Time/Date Stamp

10. File Mode and Time Stamp

11. How to compare file time stamps in condition statements?

12. file name by time stamps

13. Finding new files (create after a time stamp)