Search code examples
shelldateunixtimestamptouch

How can I modify the access timestamp of a file to seconds before using a shell command in unix?


I try to change the access time of a file to seconds before, for example to set "1437082451" (07/16/2015 @ 9:34:11pm (UTC)) to "1437082450" (07/16/2015 @ 9:34:10pm (UTC)) So I just substract one second to the current access time. I have to do so with only shell commands, I searched for a while but can't find nothing.


Solution

  • touch -at $(date -d @$(( $(stat -c '%X' "$filename") - 1)) '+%Y%m%d%H%M.%S') "$filename"
    

    That uses stat to retrieve the current access time of the file, bash arithmetic to subtract one second, and date to format the new time for the touch command.