The date format you are referring to is ISO 8601. Use the -I
option to the date
command to format dates according to this format (the s
specifies precision up to integral seconds):
$ date -Is
2013-10-08T10:48:03+0300
To obtain the last modification time of a file (in seconds since the epoch), use the %Y
format specifier with the stat
command:
$ stat -c %Y file1
1378818806
Combining these two, use date -d
to format the output of stat -c
:
$ date -Is -d @`stat -c %Y file1`
2013-09-10T16:13:26+0300
So this is the statement that does what you need:
$ date -Is -d @`stat -c %Y file1` > file2
I'm almost sure this is due to the changeover to Daylight Saving Time in the given timezone: effectively this means that an hour "disappears" (and hence becomes "invalid
").
In my own timezone, DST started at 2AM on Sunday 10th March, so that hour is invalid:
$ cat /etc/timezone
America/Toronto
$ date --date="2019-03-10 02:00:00"
date: invalid date ‘2019-03-10 02:00:00’
whereas the times immediately before and after are valid:
$ date --date="2019-03-10 01:59:59"
Sun Mar 10 01:59:59 EST 2019
$ date --date="2019-03-10 03:00:00"
Sun Mar 10 03:00:00 EDT 2019
In timezones where the change over happens at midnight, the bare date appears invalid because GNU date
assumes a time of midnight:
$ TZ=Asia/Tehran date --date='2019-03-22'
date: invalid date ‘2019-03-22’
but one hour later is valid:
$ TZ=Asia/Tehran date --date='2019-03-22 01:00:00'
Fri Mar 22 01:00:00 +0430 2019
See also Invalid Date Linux
Best Answer
If you are recursively copying you could use:
From the
cp
manual page: