Skip to main content
71 votes

Prepending a timestamp to each line of output from a command

For a line-by-line delta measurement, try gnomon. It is a command line utility, a bit like moreutils's ts, to prepend timestamp information to the standard output of another command. Useful for long-...
Janus Troelsen's user avatar
71 votes
Accepted

Running a loop precisely once per second

To stay a bit closer to the original code, what I do is: while true; do sleep 1 & ...your stuff here... wait # for sleep done This changes the semantics a little: if your stuff took less ...
frostschutz's user avatar
  • 49.5k
65 votes
Accepted

Can't set time to wee hours of January 1, 1970

This is caused by commit e1d7ba873555 ("time: Always make sure wall_to_monotonic isn't positive"). Per the commit message: This patch fix the problem by prohibiting time from being set to a ...
Joseph Sible-Reinstate Monica's user avatar
61 votes

Should I (still) use UTC for all my servers?

Is there any good reason to (still) use UTC as the system's time zone? Yes absolutely! Consider a major event that happens on 29 October 2023 at 02:30. Log messages are duly generated. (The relevance ...
Chris Davies's user avatar
50 votes

The 01:00 in 1 January 1970 at 01:00

The UK government (in its infinite wisdom) decided to experiment with Daylight Saving Time. It shifted to DST on Sunday, 18 February, 1968, and omitted the shift back that Autumn. It observed DST all ...
Paul_Pedant's user avatar
  • 8,959
45 votes

How does a Linux operating system stand going back in time (when applying winter time for example)? Going back a second, would it be the same problem?

In Linux, the operating system maintains a clock that runs fundamentally in UTC time, which does not have Daylight Saving time shifts. The (usually) one-hour Daylight Saving Time is handled by not ...
telcoM's user avatar
  • 99.5k
39 votes
Accepted

Should I (still) use UTC for all my servers?

I can really understand your log-reading pain, but I wouldn't want to discuss times in log files with my American and German colleagues during the ca 2 weeks a year where Daylights saving time has ...
Marcus Müller's user avatar
36 votes
Accepted

Why is filesystem time always some msecs behind system time in Linux?

The time used for file timestamps is the time at the last timer tick, which is always slightly in the past. The current_time function in inode.c calls ktime_get_coarse_real_ts64: /** * current_time - ...
Stephen Kitt's user avatar
33 votes
Accepted

Where is Unix Time / Official Time Measured?

Your headline question doesn't have a real answer; Unix time isn't a real timescale, and isn't "measured" anywhere. It's a representation of UTC, albeit a poor one because there are moments ...
hobbs's user avatar
  • 1,013
32 votes

Compare two file modification dates

Given that you're using the stat (similar functionality, but different output format on BSDs and GNU), you could also use the test utility, which does this comparison directly: FILE1 -nt FILE2 ...
Thomas Dickey's user avatar
31 votes

What's the POSIX-compliant way to get the epoch timestamp in a shell?

For the epoch time as an integer number of seconds, that would be: awk 'BEGIN{srand(); print srand()}' or: awk 'BEGIN{print srand(srand())}' As in POSIX awk, srand() without argument uses the ...
Stéphane Chazelas's user avatar
30 votes
Accepted

tar - How to preserve timestamps down to more than a second of precision?

The usual tar format stores timestamps as a number of seconds, as you determined. To store timestamps with more resolution, you need to use another format, such as POSIX tar as produced by GNU tar ...
Stephen Kitt's user avatar
30 votes

Running a loop precisely once per second

If you can restructure your loop into a script / oneliner then the simplest way to do this is with watch and its precise option. You can see the effect with watch -n 1 sleep 0.5 - it will show ...
Maelstrom's user avatar
  • 401
28 votes
Accepted

Current time/date as a variable in BASH and stopping a program with a script

When you do NOW=`date '+%F_%H:%M:%S'` or, using more modern syntax, NOW=$( date '+%F_%H:%M:%S' ) the variable NOW will be set to the output of the date command at the time when that line is ...
Kusalananda's user avatar
  • 338k
26 votes
Accepted

Bash: calculate the time elapsed between two timestamps

With the GNU implementation of date or compatible, this will give you the date in seconds (since the UNIX epoch) date --date '2017-08-17 04:00:01' +%s # "1502938801" And this will give ...
Chris Davies's user avatar
24 votes
Accepted

stat: modification timestamp of a file

Ubuntu uses the GNU coreutils stat, whereas OSX uses the BSD variant. So on Ubuntu the command is a bit different: stat -c %Y .bashrc From man stat: -c --format=FORMAT use the ...
Digital Trauma's user avatar
22 votes

Should I (still) use UTC for all my servers?

Servers should always store UTC. Local time is a presentation layer issue that only humans need to see. If you, as a user, want to look at some timestamped data, then you probably want to see it in ...
Neil's user avatar
  • 329
22 votes

Why is filesystem time always some msecs behind system time in Linux?

Stephen Kitt's answer seems to be spot-on. We can reproduce this very nicely by actually getting the same "coarse" clock that the filesystem uses, at least on my kernel configuration; a C ...
Marcus Müller's user avatar
20 votes
Accepted

Convert Unix timestamp to human-readable time

If the line starts with the unix timestamp, then this should do it: perl -pe 's/^(\d+)/localtime $1/e' inputfilename perl -p invokes a loop over the expression passed with -e which is executed for ...
wurtel's user avatar
  • 16.2k
20 votes

date - Can't Go Back More Than 115 Years or Can't Go 5879565 Years Into the Future

There are lots of almost-compatible ways of counting time and date. The 'traditional' unix-time counts seconds since 1970 (a.k.a 'the epoch'). This has a huge problem in 32-bit representations, where ...
Popup's user avatar
  • 616
18 votes

Convert Unix timestamp to human-readable time

If your AWK is Gawk, you can use strftime: gawk '{ print strftime("%c", $1) }' will convert the timestamp in the first column to the current locale’s default date/time representation. You can use ...
Stephen Kitt's user avatar
17 votes
Accepted

Timestamps of files copied to USB drive

The problem with the timestamp seconds changing comes from the fact that a VFAT (yes, even FAT32) filesystem stores the modification time with only 2-second resolution. Apparently, as long as the ...
telcoM's user avatar
  • 99.5k
16 votes
Accepted

will `ls -lt` follow the accurate mtimes or just the approximate mtimes up to second?

That very much depends on the ls implementation. Of those 4 found on a GNU/Linux system here: $ touch a; touch c; touch b; stat -c %y a c b 2018-01-10 12:52:21.367640342 +0000 2018-01-10 12:52:21....
Stéphane Chazelas's user avatar
16 votes
Accepted

Different timestamps in SFTP "ls -l" vs. "ls -lh"

When listing a directory using SFTP protocol, the server provides both structured metadata of files (name, timestamp, size, etc) and textual listing of files. The OpenSSH sftp with -l switch prints ...
Martin Prikryl's user avatar
15 votes

Where is Unix Time / Official Time Measured?

UNIX time is measured on your computer, running UNIX. This answer is going to expect you to know what Coördinated Universal Time (UTC), International Atomic Time (TAI), and the SI second are. ...
JdeBP's user avatar
  • 69.3k
15 votes
Accepted

Directory "recursive" last modified date

One option: use GNU find to recurse through all files; print timestamp with filepath and sort by date: find /path/mydir -type f -printf "%T+\t%p\n" | sort | tail -1 For just the epoch ...
user4556274's user avatar
  • 9,045
15 votes

How can I use files from HTTP as prerequisites in GNU make?

Try something like this in your Makefile: .PHONY: local.dat local.dat: [ -e example.gz ] || touch -d '00:00' example.gz curl -z example.gz -s http://example.org/example.gz -o example.gz [...
cas's user avatar
  • 79.1k
14 votes

Bash: calculate the time elapsed between two timestamps

This is easy with datediff command provided in dateutils package. datediff -i '%Y%m%d%H%M%S' 20170817040001 20160312000101 See the download page fotlr the latest package and installation file to how ...
αғsнιη's user avatar
  • 41.5k
14 votes
Accepted

Performing -nt/-ot test in a POSIX sh

POSIXLY: f1=/path/to/file_1 f2=/path/to/file_2 if [ -n "$(find -L "$f1" -prune -newer "$f2")" ]; then printf '%s is newer than %s\n' "$f1" "$f2" fi Using absolute path to files prevent a false ...
cuonglm's user avatar
  • 155k

Only top scored, non community-wiki answers of a minimum length are eligible