How does touch -t
command works exactly, internally (I tried to find its source code but couldn't)?
How does touch -t work internally
datefilestimestamps
Related Solutions
touch
creates a new, empty file if the file doesn't exist because that's what it was designed to do. The utility has to contain code to handle that case specifically. The utility appeared in Unix V7; its manual described it thus:
touch — update date last modified of a file
touch
attempts to set the modified date of each file. This is done by reading a character from the file and writing it back. If a **file* does not exist, an attempt will be made to create it unless the-c
option is specified.
(I don't know what touch
did if the file was empty. The underlying system call came later.)
I don't know for sure why touch
was designed to make the file exist, but I suspect it's because of make
. Why would you want to set a file's modification time to the current time? There are cases where it could be useful to set the modification time to a particular time, but that ability came later, the original touch
could only set the modification time to the current time. A reason to do that is to re-run a make
rule that depends on the file.
That is, suppose you have a file foo
, and a makefile that declares a command to generate bar
from foo
. When you type make bar
, the command is executed and bar
is created. If bar
exists and is newer than foo
, make bar
does nothing, because make
assumes that bar
has already been generated. However, if bar
is older than foo
, make thinks that bar
is not up-to-date and needs to be regenerated.
But what if the rules to generate bar
have changed? Then you have two options:
rm bar; make bar
touch foo; make bar
You would need foo
to exist in order to generate bar
, otherwise the command would typically not work.
The “touch” terminology was also present in the make
utility: make -t bar
would only pretend to run the commands, that is, it would set the modification time of bar
to the current time without actually running the command to generate bar
(you would do this if you thought that the changes to foo
shouldn't affect bar
). The touch
utility was therefore a standalone version of the make -t
feature.
-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
Best Answer
touch
calls theutimes
system call to set the file's modification time and its access time. On some systems, instead ofutimes
, it opens the file and then sets the file times through the descriptor, e.g. withutimensat
under Linux.You can see how
touch
works on your system by looking at the system calls it makes. Under Linux, use strace, e.g.strace touch -d '1 hour ago' foo
.Where to find the source code depends on your operating system. The GNU version is in coreutils, there's a version in the main source tree of any BSD, there's a version in BusyBox, in Minix, etc.