2

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?

  • @jsotola Well, I don't understand the question - or maybe I don't know enough about it. I can set it to the current time or to 1st January 1970 using -t 197001010000, but I don't know what I'm actually meant to do and I'm being very stupid right now. – Mandingo Nov 23 '18 at 9:47
  • ????? I don't understand the question ... is this a school assignment? – jsotola Nov 23 '18 at 9:51
  • @jsotola Not exactly, but it's a question I was set to do in an introductory programme to Linux (There's a bunch of other questions, but I just don't exactly understand or know how to do this one as I'm a beginner in Linux and almost never used it before, and the wording is throwing me off.) – Mandingo Nov 23 '18 at 9:54
  • read your own sentence above, the one starting with I understand ... ....... now think, when is unix epoch zero? ..... when is it 10? .... when is it 20? ......... is this correct ? 1st January 1970 using -t 197001010000 – jsotola Nov 23 '18 at 9:58
  • @jsotola So, I'm meant to get the value of the Unix epoch (amount of seconds that has passed since 01 January 1970) and use that value in the command? I don't know how to get the Unix epoch value in Linux or how to put it in the touch command – Mandingo Nov 23 '18 at 10:00
4

-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
  • Also, is touch -m -d ”@$(date +%s)” test.txt a correct way of doing it? – Mandingo Nov 23 '18 at 10:23
  • 1
    yes if you want to change modify time only, without it it will change access time also – rAlen Nov 23 '18 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.