使用 touch 命令将文件修改时间设置为 Unix 纪元

Man*_*ngo 3 linux date touch

编辑:措辞更好的问题:如何仅使用 touch 命令将文件的修改时间设置为 unix 纪元?

我知道可以使用“data %s”检索 unix epoch 值,但是如何使用 touch 命令(并且仅使用该命令)将修改时间设置为 unix epoch?

编辑2:

所以,我发现这运行没有任何错误:

touch -m -d ”@$(date +%s)” fileexample.txt
Run Code Online (Sandbox Code Playgroud)

这是将文件的修改时间设置为 Unix 纪元的正确方法吗?



原始问题(无视)...:

Using the Linux manual for the “touch” command, show the command that you would 
use to set the modification time of a file to the Unix epoch.
Run Code Online (Sandbox Code Playgroud)

我知道 Unix 纪元是自纪元(1970 年 1 月 1 日)以来经过的秒数(或毫秒,我忘记了)

时间设置“:是什么的问题对我们说Unix纪元”?

那么,它基本上是在询问今天的时间,还是 1970 01 01,或者……?

我知道这个命令是:

touch -m -t time file
Run Code Online (Sandbox Code Playgroud)

但是我把它设置为什么时间?

另外,我是否打算在命令中使用 unix epoch 格式?

rAl*_*len 7

-t不接受纪元时间,-d确实

   -d, --date=STRING
          parse STRING and use it instead of current time

   -t STAMP
          use [[CC]YY]MMDDhhmm[.ss] instead of current time
Run Code Online (Sandbox Code Playgroud)

您需要使用-dor--date代替-t并且需要@在使用纪元时间格式之前放置,如date联机帮助页中所述:

   EXAMPLES
       Convert seconds since the epoch (1970-01-01 UTC) to a date

              $ date --date='@2147483647'
Run Code Online (Sandbox Code Playgroud)

例子:

touch --date=@1442968132 test.txt
Run Code Online (Sandbox Code Playgroud)

如果你想改变修改时间只,使用-m--time modify或者--time mtime,如果没有它都修改和访问时间被改变。

   -m     change only the modification time

   --time=WORD
          change the specified time: WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m
Run Code Online (Sandbox Code Playgroud)

例子:

$ touch --date=@1442968132 test
$ stat test
  File: test
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd03h/64771d    Inode: 43266017    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/    user1)   Gid: ( 1000/    user1)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2015-09-23 02:28:52.000000000 +0200
Modify: 2015-09-23 02:28:52.000000000 +0200
Change: 2018-11-23 11:34:59.893888360 +0100
 Birth: -

$ touch --date=@1542968132 test
$ stat test
  File: test
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd03h/64771d    Inode: 43266017    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/    user1)   Gid: ( 1000/    user1)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2018-11-23 11:15:32.000000000 +0100
Modify: 2018-11-23 11:15:32.000000000 +0100
Change: 2018-11-23 11:35:06.893888073 +0100
Birth: -

$ touch -m --date=@1342968132 test
$ stat test
  File: test
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd03h/64771d    Inode: 43266017    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/    user1)   Gid: ( 1000/    user1)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2018-11-23 11:15:32.000000000 +0100
Modify: 2012-07-22 16:42:12.000000000 +0200
Change: 2018-11-23 11:35:22.300887441 +0100
Run Code Online (Sandbox Code Playgroud)