Date format in UNIX
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
You can display a date that way with this command:
$ date +%Y-%m-%dT%H:%M:%S%z
2013-10-08T07:38:45+0200
Many file systems do not store a file creation date so there is not always a method to get it. If the file has never been modified since its creation, this would work:
$ date -r file1 +%Y-%m-%dT%H:%M:%S%z > file2
$ cat file2
2013-10-08T07:32:52+0200
Combining my previous answer and that of @jlliagre, the most concise way to do what you want is:
date -Is -r file1 > file2
Does this help you?
ls -c -l file1 --time-style="+%Y-%m-%dT%H:%M:%S%z" > file 2