Skip to main content
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
  • 52.1k
66 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
Accepted

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
  • 9,414
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
  • 114k
40 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
34 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
32 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 executed....
Kusalananda's user avatar
  • 356k
32 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

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
27 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
23 votes
Accepted

File has been changed, but its "date modified" is the same. How is that possible?

You will notice that test.app is a directory: $ tree test.app test.app └── Contents ├── Info.plist ├── MacOS │   └── applet ├── PkgInfo ├── Resources │   ├── Scripts │   │  ...
Kusalananda's user avatar
  • 356k
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
21 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.5k
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
  • 714
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
  • 114k
17 votes
Accepted

Is there a reason why I can't use find to scan modified files for viruses and malware?

It's probably not the worst idea as long as you also do full system scans regularly in addition to your incremental scans, however there are some downsides: Not all exploits require file modifications ...
jesse_b's user avatar
  • 41.5k
16 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,405
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

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
  • 84k
15 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
  • 158k
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.9k
13 votes

Copy file creation date to metadata in ffmpeg

A part of answer using exiftool. exiftool -tagsFromFile inputfile.mov -MediaCreateDate outputfile.mp4 This could be done after ffmpeg conversion. This rely not on file modification time but rather ...
Colin Brosseau's user avatar
13 votes

Moving directory without modifying timestamp

Use cp as following, mv doesn't do. cp -r -p /path/to/sourceDirectory /path/to/destination/ from man cp: -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the ...
αғsнιη's user avatar
  • 41.9k
13 votes

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

Write a C program that calls time() and prints the result. Borrowing from sample program in the specification of the time() function, let's call this e.g. seconds.c: #include <stdio.h> #include &...
ilkkachu's user avatar
  • 148k

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