Linux – Use touch command to set modification time of a file to the Unix epoch

datelinuxtouch

Edit: Better worded question: How do I only use the touch command to set the modification time of a file to the unix epoch?

I know that the unix epoch value can be retrieved using "data %s", but how do I use the touch command (and only that command) to set the modification time to the unix epoch?

Edit2:

So, I found that this runs without any errors:

touch -m -d ”@$(date +%s)” fileexample.txt

Is this a correct way of setting the modification time of a file to the Unix epoch?



Original Question (disregard)…:

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.

I understand that the Unix epoch is the amount of second (or miliseconds, I forgot) that has passed since the epoch (1970, January, 01)

What does the question mean by saying: setting the time "to the Unix epoch"?

So, Is it basically asking for today's time, or 1970 01 01, or…?

I know the command for this would be:

touch -m -t time file

But what time do I set it to?

Also, am I meant to use the unix epoch format for the time in the command?

Best Answer

-t doesn't accept epoch time, -d does

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

   -t STAMP
          use [[CC]YY]MMDDhhmm[.ss] instead of current time

You need to use -d or --date instead of -t and you need to put @ before epochtime format is used, as described in date manpages:

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

              $ date --date='@2147483647'

Example:

touch --date=@1442968132 test.txt

If you want to change modify time only, use -m or --time modify or --time mtime, without it both modify and access times are changed.

   -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

Examples:

$ 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
Related Question