How to read and write last modified timestamp to files?

Need help reading file last modified date in format:

Filename (relative path);YYYYMMDDHHMMSS

And then write it back. My idea is to backup it to a text file to restore later.

Checked this command but does not work:
Getting the Last Modification Timestamp of a File with Stat

$ stat -f "%Sm" -t "%Y%m%d_%H%M%S" filename.txt
stat: cannot read file system information for `%Sm': No such file or directory
stat: cannot read file system information for `%Y%m%d_%H%M%S': No such file or directory
filename.txt 6cf4032d 255 3e700ff 4096 4096 29171199 13977810 13977810 18446744073709551615 -1

Here's one way to do it:

[user@host ~]$ date -d @$(stat -c%Y file) +"%Y%m%d%H%M%S"
20140118220547
[user@host ~]$
1 Like

You combined file and file system stat queries, which does not work. And you gave a format descriptor without the -c or --printf option (on top of the fact that there's no date/time info for file systems)

1 Like

This example works perfectly on Cygwin, however it doesn't on android, where timestamps are always read and written in unix time from epoch.

  1. The %Y format specifier in stat -c command gives the time output in unix time from epoch.
  2. I tried the command on Terminal IDE app (BusyBox v1.19.2) installed on my android phone (running Android v4.3) and it works just fine. Here's a screenshot.

1 Like
$ date -d @$(stat -c%Y file) +"%Y%m%d%H%M%S"
unknown option -- ddate: invalid option -d