COTD: touch - change file timestamps

Todays COTD (Command Of The Day) is the “touch” command.

The touch command is used to update the access and modification times of files.  However, I see it more commonly used to create a new file.  There are many times when you want to create a new file, but not actually put anything in it.  For example, if you have a script that checks for the presence of a file to control some logic.  In Solaris, if you “touch” a file called “/reconfigure”, it will perform a reconfiguration boot the next time you reboot the system.

Touch can also be used to update the access, or modification times, of a file.  This might be useful if you are using a file’s access or modification time to see if other files are newer than this reference file.

For example:

$ touch file1
$ touch file2
$ ls -l
total 0
-rw-r–r– 1 tuz tuz 0 2009-03-02 05:07 file1
-rw-r–r– 1 tuz tuz 0 2009-03-02 05:07 file2

Here we have created two files called file1 and file2.

$ touch timestamp
$ ls -l
total 0
-rw-r–r– 1 tuz tuz 0 2009-03-02 05:07 file1
-rw-r–r– 1 tuz tuz 0 2009-03-02 05:07 file2
-rw-r–r– 1 tuz tuz 0 2009-03-02 05:09 timestamp

We now have three files. Now, lets run a find command and look for files that are newer than our timestamp file.

$ find . -cnewer timestamp

There are none found. This is no surprise. Now, lets create another new file, and then run the same find command again.

$ touch file3
$ find . -cnewer timestamp
.
./file3

This time, find shows us the new file that is newer than the reference file. This could be handy for a number of uses.  For example, you could use this to quickly find out what files have changed since the last time you created a backup of your home directory, among many other uses.

Find also has the ability to search files files that have been accessed more recently than a reference file.    Please check the man page out for your local version of touch.

Some other options for touch include:

  • Change only the access time
  • Set the date/time to a user defined date/time
  • Change only modification time

Please check the man page for your local version of touch as some versions may be different.

Post a Comment

Your email is never shared. Required fields are marked *

*
*