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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 - ...
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 ...
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....
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 ...
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 ...
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 ...
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
│ │ ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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
...
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 ...
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....
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 ...
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
[...
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 ...
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 ...
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 ...
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 ...
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 &...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
timestamps × 586files × 160
bash × 84
date × 78
shell-script × 71
linux × 63
find × 48
shell × 40
logs × 32
ls × 31
filesystems × 28
time × 28
stat × 23
awk × 21
rsync × 21
command-line × 19
directory × 18
text-processing × 17
sort × 14
timezone × 14
touch × 14
ubuntu × 12
macos × 12
file-copy × 12
scripting × 11