touch
touch
is a utility designed for updating the access and modifications times of one or more files. More commonly, however, it is used to simply create a blank file.
To use touch to create an empty file, simply run touch
followed by the absolute, or relative path to the file you wish to create.
touch /home/kamstut/test.txt
Just like that, an empty file called test.txt
will be created in the /home/kamstut
directory.
The actual purpose of this utility is to change the access and modification times of a given file. By default, if you use touch
to create a new file, the new file will be created with the current time as its access and modification times.
touch /home/kamstut/test.txt
stat /home/kamstut/test.txt
File: ‘test.txt’ Size: 0 Blocks: 0 IO Block: 524288 regular empty file Device: 2dh/45d Inode: 501326713 Links: 1 Access: (0600/-rw-------) Uid: (209239/ kamstut) Gid: (20118/tdm-admin) Access: 2021-08-31 13:37:42.225731000 -0400 Modify: 2021-08-31 13:37:42.225731000 -0400 Change: 2021-08-31 13:37:42.225731000 -0400 Birth: -
Here, you can see that test.txt
was created on August 31, 2021 at 13:37:42.225731000, and the access and modification times were set to the current time. You can even see what time zone the system is in.
To modify the modification time, and only the modification time, use the -m
option. By default, this will change the modification time to the current time.
touch -m /home/kamstut/test.txt
stat /home/kamstut/test.txt
File: ‘test.txt’ Size: 0 Blocks: 0 IO Block: 524288 regular empty file Device: 2dh/45d Inode: 501326713 Links: 1 Access: (0600/-rw-------) Uid: (209239/ kamstut) Gid: (20118/tdm-admin) Access: 2021-08-31 13:37:42.225731000 -0400 Modify: 2021-08-31 14:01:45.387737252 -0400 Change: 2021-08-31 14:01:45.390033541 -0400 Birth: -
To change the modification time to a time other than the current time, use the -t
option.
touch -m -t "201908311401.45" /home/kamstut/test.txt
stat /home/kamstut/test.txt
File: ‘test.txt’ Size: 0 Blocks: 0 IO Block: 524288 regular empty file Device: 2dh/45d Inode: 501326713 Links: 1 Access: (0600/-rw-------) Uid: (209239/ kamstut) Gid: (20118/tdm-admin) Access: 2021-08-31 13:37:42.225731000 -0400 Modify: 2019-08-31 14:01:45.000000000 -0400 Change: 2021-08-31 14:07:19.219404903 -0400 Birth: -
To change only the access time, similarly to the -m
option, use the -a
option. By default, with no -a
option and no -m
option, both the access and modification times will be set to the current time.
Lastly, to change an access and/or modification time to match another file’s values, use the -r
option.
touch /home/kamstut/test2.txt
stat /home/kamstut/test2.txt
File: ‘test2.txt’ Size: 0 Blocks: 0 IO Block: 524288 regular empty file Device: 2dh/45d Inode: 480486061 Links: 1 Access: (0600/-rw-------) Uid: (209239/ kamstut) Gid: (20118/tdm-admin) Access: 2021-08-31 14:11:28.299266000 -0400 Modify: 2021-08-31 14:11:28.299266000 -0400 Change: 2021-08-31 14:11:28.299266000 -0400 Birth: -
Now to change our new test2.txt
file to match the same access and modification times as our original test.txt
file, we can use the -r
option.
touch /home/kamstut/test2.txt -r /home/kamstut/test.txt
File: ‘test2.txt’ Size: 0 Blocks: 0 IO Block: 524288 regular empty file Device: 2dh/45d Inode: 480486061 Links: 1 Access: (0600/-rw-------) Uid: (209239/ kamstut) Gid: (20118/tdm-admin) Access: 2021-08-31 13:37:42.225731000 -0400 Modify: 2019-08-31 14:01:45.000000000 -0400 Change: 2021-08-31 14:12:32.617721750 -0400 Birth: -