LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash: how to get time (in seconds) elapsed since creation of a file? (https://www.linuxquestions.org/questions/linux-general-1/bash-how-to-get-time-in-seconds-elapsed-since-creation-of-a-file-178335/)

J_Szucs 05-05-2004 06:32 PM

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?

hw-tph 05-05-2004 07:18 PM

Try ls --full-time.


Håkan

J_Szucs 05-05-2004 07:44 PM

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.

hw-tph 05-06-2004 02:00 AM

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

J_Szucs 05-07-2004 03:11 AM

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.

hw-tph 05-07-2004 04:56 AM

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. :D

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

wastingtime 12-27-2008 11:28 PM

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


All times are GMT -5. The time now is 07:44 PM.