The touch command does more than just create empty files
Beginners to Unix/Linux learn about the touch command as a way to create an empty file.
$ ls -l /tmp/foo ls: /tmp/foo: No such file or directory $ touch /tmp/foo $ ls -l /tmp/foo -rw-r--r-- 1 andy wheel 0 Jul 10 11:56 /tmp/foo
But there’s more to it than that. The main job of touch is to modify the timestamps on a file. Creation of a file is almost a side effect.
The -t argument to touch lets me specify a date and time to set on the file.
$ ls -l /tmp/foo ls: /tmp/foo: No such file or directory $ touch -t 201107010930 /tmp/foo $ ls -l /tmp/foo -rw-r--r-- 1 andy wheel 0 Jul 1 09:30 /tmp/foo
If I don’t specify a date and time, then the current date and time are used.
$ ls -l /tmp/foo -rw-r--r-- 1 andy wheel 0 Jul 1 09:30 /tmp/foo $ touch /tmp/foo $ ls -l /tmp/foo -rw-r--r-- 1 andy wheel 0 Jul 10 11:58 /tmp/foo
You can also use the timestamp from another file and apply it to another file by using the -r switch.
$ ls -l /tmp/someotherfile -rw-r--r-- 1 andy wheel 0 Aug 12 2005 /tmp/someotherfile $ touch -r /tmp/someotherfile /tmp/foo /tmp/foo2 /tmp/foo3 $ ls -al /tmp/ -rw-r--r-- 1 andy wheel 0 Aug 12 2005 /tmp/foo -rw-r--r-- 1 andy wheel 0 Aug 12 2005 /tmp/foo2 -rw-r--r-- 1 andy wheel 0 Aug 12 2005 /tmp/foo3 -rw-r--r-- 1 andy wheel 0 Aug 12 2005 /tmp/someotherfile
man touch will give you all the details.
6 responses