LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-05-2004, 07:32 PM   #1
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
Bash: how to get time (in seconds) elapsed since creation of a file?


How to get the time (in seconds) elapsed since creation of a file (created by an other process)?

I thought I might use stat, but it is an other unix, where "which stat" returns nothing.
Maybe I need some other command that can print file creation times in UTC format, then I could create a timestamp file, and subtract the creation times of the two files...

But which command can give file creation times in UTC?
 
Old 05-05-2004, 08:18 PM   #2
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
Try ls --full-time.


Håkan
 
Old 05-05-2004, 08:44 PM   #3
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Original Poster
Rep: Reputation: 58
Thanks, but
ls --full-time
only displays the time in a more precise format, but it is still not UTC.

In the meantime, I figured out a workaround. Getting the h,m,s values from the ls -lT output by awk, then multiplying them with 3600, 60 and 1, respectively, would give me the time (seconds) elapsed until file creation, reckoned from midnight. Doing the same with the timestamp file would make it possible to calculate the difference.

However, this requires many processes, and I wonder if there is a smarter way to do this.

Last edited by J_Szucs; 05-05-2004 at 08:50 PM.
 
Old 05-06-2004, 03:00 AM   #4
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
To force UTC, simply set $TZ to UTC: TZ="UTC" ls <options>. This will set $TZ to UTC just for the duration of this command. Set it globabally if this is what you want as per default.
Code:
hw@baron:~/tmp/newtmp$ ls --full-time
total 0
-rw-r--r--    1 hw       users           0 2004-05-06 09:01:20.071583048 +0200 newfile
-rw-r--r--    1 hw       users           0 2004-05-06 09:01:16.854072184 +0200 oldfile
hw@baron:~/tmp/newtmp$ TZ="UTC" ls --full-time
total 0
-rw-r--r--    1 hw       users           0 2004-05-06 07:01:20.071583048 +0000 newfile
-rw-r--r--    1 hw       users           0 2004-05-06 07:01:16.854072184 +0000 oldfile
hw@baron:~/tmp/newtmp$

Håkan

Last edited by hw-tph; 05-06-2004 at 03:02 AM.
 
Old 05-07-2004, 04:11 AM   #5
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Original Poster
Rep: Reputation: 58
Well, I think there is a little misunderstanding between us.
What I need is a time format that is the best for numeric calculations.

So far I thought that UTC is such, as it represents the current time as a pure number, i.e. the number of seconds elapsed since 1st January 1970 (or 1971?) 0h0m0s. (This is the time format that squid uses by default in its log files.)

In this meaning, the examples in your post do not use UTC time format, but simply give the current time in a different time zone.

Am I wrong?

In the meantime, however, I finished my script using the idea I described in my previous posts, and I got faced with a funny BASH bug:

When I cut out the hours, minutes and seconds from the output of 'ls -l', and use it for numeric calculations like this:

let "secondssincemidnight = $hours * 3600 + $minutes * 60 + $seconds"

the above expression fails and results in an error when either variable $hours, $minutes or $seconds has a value of "08" or "09". Any other values are treated well.
Of course I know the workaround: I have to remove all of the leading 0s, but I s[u]cked a lot until I realised that the eventual failures of my code are not due to a bug in my code, but a not so wide-known bug of my bash version is the culprit.

Last edited by J_Szucs; 05-07-2004 at 04:41 AM.
 
Old 05-07-2004, 05:56 AM   #6
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
No, you're not wrong, I just completely misinterpreted your post. Blame it on my poor English or my poor brain, but it was my fault indeed.

UTC really is a timezone though - it is defined as solar time on meridian 0 - but I have no idea how to fool ls into providing the time in Unix/UTC format without resorting to gawking.


Håkan
 
Old 12-28-2008, 12:28 AM   #7
wastingtime
Member
 
Registered: Sep 2004
Posts: 86

Rep: Reputation: 17
use the date command

the closest thing to what you ask is:

date --utc --reference=$file +%s

it will give you the last modification time of the file in seconds since the epoch.

date --utc +%s

returns the current time in same units
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find elapsed time innuendo_98 Programming 2 11-10-2005 09:03 AM
list file creation time allelopath Linux - General 2 08-02-2005 10:02 PM
Time of creation of a file proxyserver Linux - Software 2 04-11-2004 11:29 AM
How to set file creation time in Linux Ian Knight Programming 1 05-06-2003 06:47 PM
getting elapsed time.. Mad_C Programming 2 04-01-2003 02:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration