Unix Snippet - Get File's "Touch Date-Format" One-Liner

Posted at

Pipelining the modification date from stat into an inline-date-phrase using the date --date="@... directive (using read to locate the pipeline's output in the middle of the date-phrase),
And you've got yourself a nice little converter, which you can customise, to use *any* date-format,
I've using the Universal Time Coordinated standard.

file name:          my_file.txt
unix time-format: 1458036575 (Asia/Jerusalem)
touch time-format: 201603151009 (UTC)

stat --format=%Y "my_file.txt" | { read -r tmp_date; date --utc --date="@$tmp_date" "+%C%y%m%d%H%M"; }

You can easily use the touch-date-format with touch command using the following directive:
touch -t 201603151009 --time=mtime my_file.txt;
but make sure first you set the local-timezone to same timezone it was generated on.
TZ=UTC will help you do that,
TZ=UTC; touch -t 201603151009 --time=mtime my_file.txt; will apply it to the current line.
and you can also make sure of it is done right by using:
export TZ=UTC at the start of the script, and at the end of it just re-run
export TZ=Asia/Jerusalem (or whatever your TimeZone is) to come-back to your TimeZone,