Touch (Unix) - Examples

Examples

The simplest use case for touch is thus:

$ touch myfile.txt

Touch doesn't modify the contents of myfile.txt; it just updates the timestamp of the file to the computer's current date and time, whatever that happens to be. Or, if myfile.txt does not exist it is created, with zero length.

Here's an example that shows why we might want to do this. We wish to re-make a software project we are writing. We have changed the makefile and need to run make again. However, if we run make immediately we find that

$ make make: nothing to be done for `all'

Since the source code file is already updated, we will need to use touch to simulate a file update, so make will run and recompile the software.

$ touch project.c $ make

Then make will rebuild the project.

Here's how to change the date and time of a file.

$ touch -t 200701310846.26 index.html $ touch -d '2007-01-31 8:46:26' index.html $ touch -d 'Jan 31 2007 8:46:26' index.html

The above example touch commands are equivalent: they will change the date and time of index.html to January 31, 2007 at 8:46:26am.

The creation date of links are unchanged. For example, on the following system, the date is the 20th Feb 2012, but a link was created on 25th Jan 2012. Despite touching the link, the date of this remains at 22nd Jan 2012 - it not changed to the 20th Feb 2012.

$ date Wed Feb 20 09:45:50 GMT 2012 $ ls -l libcidn.so lrwxrwxrwx 1 foobar foobar 22 Jan 25 01:41 libcidn.so -> ../../lib/libcidn.so.1 $ touch libcidn.so $ ls -l libcidn.so lrwxrwxrwx 1 foobar foobar 22 Jan 25 01:41 libcidn.so -> ../../lib/libcidn.so.1

Although commands like cp, grep, chmod etc. have a recursive switch (-r or -R or both) to apply the command recursively to the subdirectories, touch doesn't have this functionality yet (as of February, 2012). It can be accomplished by the following:

$ find . -exec touch {} +

Read more about this topic:  Touch (Unix)

Famous quotes containing the word examples:

    No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.
    André Breton (1896–1966)

    It is hardly to be believed how spiritual reflections when mixed with a little physics can hold people’s attention and give them a livelier idea of God than do the often ill-applied examples of his wrath.
    —G.C. (Georg Christoph)

    There are many examples of women that have excelled in learning, and even in war, but this is no reason we should bring ‘em all up to Latin and Greek or else military discipline, instead of needle-work and housewifry.
    Bernard Mandeville (1670–1733)