0

According to many websites I found online the touch commands' -t argument accepts the time stamp in the following format:

[[CC]YY]MMDDhhmm[.ss] Here, CC: The first two digits of the year. YY: The last two digits of the year. MM: Month DD: Day of the month hh: Hour mm: Minute ss: Seconds 

For example:

$ touch -t 199901011200 test.txt 

Can I use the date or stat command to obtain the timestamp in the same format from a file or directory? By same format I mean the format above [[CC]YY]MMDDhhmm[.ss] By default the date command has a different output.

Note: I do not want to use touch reference -r command.

1 Answer 1

2

Using date you specify the following format:

date -r test.txt +'%Y%m%d%H%M.%S' 
  1. %Y: Year (with four digits)
  2. %m: Month number (with two digits)
  3. %d: Day (with two digits)
  4. %H: Hour (from 00 to 23)
  5. %M: Minutes
  6. %S: Seconds

You can check man date to more information.

2
  • Thank you. I think the ss in [[CC]YY]MMDDhhmm[.ss] is different from what the date command's %S outputs because I get this error when I pipe the date ouput into touch: touch: invalid date format ‘20220901123513’. I'm wondering if touch uses the seconds since the epoch which is %s in date. That works. I'm just not sure what they mean in [.ss] Commented Oct 17, 2022 at 8:37
  • 1
    @ZoltanKing I've updated the answer. With touch you should add a . before the seconds. Commented Oct 17, 2022 at 8:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.