touch: manipulative and useful

The touch tool is a very basic one that can also be very helpful. If you are in a POSIX compliant environment (unix/linux/osx) then you will most likely have this tool built-in. touch at the core is a tool that creates files. 0-byte files to be exact. If you execute the touch tool giving only the name of a file (non-existent) then a file will be created with said name. To explain the tool a little better we will use an Apple OS X (10.5) environment to play with

1
touch /Users/alfred/Desktop/my-new-doc.docu

There are two important things to notice in the above line. One is that I gave no arguments to touch other than the path to the file. Two is that I named the file with a four character extension. Now, this is something of importance: file extensions do not matter in a POSIX environment in most every case. I will write another post on that later, but please take note on the fact that they mean nothing ;)

Now. What we did there was created a zero byte file named my-new-doc.docu on my desktop. We can list the contents of my desktop and see that the file is in fact zero bytes:

[code lang="bash" highlight="12"]
alfreds-macbook-pro:~ alfred$ ls -ltr /Users/alfred/Desktop/
total 9174280
-rw-rw-r-- 1 alfred staff 4693598208 Aug 25 2008 h-lcbtrhel5_r.iso
-rw-r--r-- 1 alfred staff 2381114 Oct 15 2008 DSC01258.JPG
-rw-r--r--@ 1 alfred staff 617742 Feb 4 13:56 IMG_0236.jpg
-rw-r--r--@ 1 alfred staff 418850 Feb 26 22:13 IMG_0985.JPG
-rw-r--r--@ 1 alfred staff 585 Apr 27 10:57 stugots.rtf
-rw-r--r--@ 1 alfred staff 23552 Apr 29 14:52 Rainbow Spreadsheet.xlt
drwxr-xr-x 9 alfred staff 306 Apr 29 14:53 live-stuff
-rw-r--r--@ 1 alfred staff 390 May 7 11:09 synuse.py
-rw-r--r-- 1 alfred staff 98 May 13 19:44 new.txt
-rw-r--r-- 1 alfred staff 0 May 15 11:00 my-new-doc.docu
alfreds-macbook-pro:~ alfred$

ok. Now that I have shown you what touch does when a filename is given with no other arguments. Also, the file didn't exist. What if the file does exist . . . . . :

1
2
3
4
alfreds-macbook-pro:~ alfred$ touch /Users/alfred/Desktop/my-new-doc.docu
alfreds-macbook-pro:~ alfred$ ls -ltr /Users/alfred/Desktop/my-new-doc.docu
-rw-r--r--  1 alfred  staff  0 May 15 11:20 /Users/alfred/Desktop/my-new-doc.docu
alfreds-macbook-pro:~ alfred$

Can you figure out what happened there? Look at line #3 above. If you are unfamiliar with the ls tool ('list'), it is showing the modification time and that modification time is different from the first time we listed the file. What does that mean?

It means that simply changed the modification time of the file. It did not destroy the contents of the file. It didn't recreate the file. It merely changed the modification time to match the exact moment the tool was executed. You might ask why this is useful and to be quite honest. . . . it isn't really. What is useful are the optional arguments that the touch tool allows :)

The best argument and the only one that I am going to cover is -t

-t allows you to specify the modification time. That means you can say I would the "last modified" time to be June 18th, 1938 at 12:46 pm EST. Now I think everyone will be able to tell how this can be useful. Let see how we can use this:

1
touch -t 193806181246 /Users/alfred/Desktop/my-new-doc.docu

Take note of the data format that I gave -t argument. When using -t you should format your time as follows:

Century Year Month Date Hour Minute Second*
19 38 06 18 12 46 29


*seconds are entirely optional. I didn't even use them, but I wanted to show that you can if you needed to

This is what the file looks like in Finder inside of OSX:

touch results in finder

touch results in finder

Now I think everyone can see how effective, helpful and manipulative the touch tool can be. Whether you just need to make an empty file of any kind or need to send a file to someone that you did a long time ago . . . . . touch is the tool for you.



One Comment

  1. Hi, first I want to say great blog. I don’t always agree with your opinion but it’s always a interesting read.
    Keep up the nice blogging.