2

CentOS 6

I'm studying RHEL / Centos and recently learned that the touch command can be used to change the last access date of a file. I'm struggling to understand a practical reason why anyone would want to do this (without actually making any changes to a file). Can someone please elaborate?

2
  • 1
    atime does have limited usefulness, and many server administrators disable it. touch can also update mtime.
    – jordanm
    Feb 16, 2013 at 17:56
  • 1
    touch(1) adjusts both the last access and the last modification time, you are generally interested in the later. For example, touch xyz.c has the effect of marking xyz.c just modified, so for example make(1) will rebuild whatever depends on it.
    – vonbrand
    Feb 16, 2013 at 18:36

4 Answers 4

3

The touch command is useful when developing software that is built with GNU Make or a similar program. make works by comparing the timestamp on files to figure out what needs to be compiled; by using touch, you can force make to rebuild a file that has not changed.

2

There can be many reasons to use touch command.

Here are simple examples including " How to impress your Boss !!"

Common Time-Stamp Problem in development environment like this.

There can be even more reasons that's why touch is so popular and useful.

2

Interrogating the last access timestamp (atime) can be useful in debugging as well as in security audits in the cases where the filesystem has been mounted to allow the metadata update (i.e. the noatime mount option has NOT been used).

To this end, some backup software will (or can be configured) to reset the atime to what it was before the file was backed-up, thus preserving the inherent usefulness of the metadata. In this case, the underlying operating system call, utime is used, upon which the touch shell utility is based.

A user who issues touch myfile would update the 'mtime', 'atime' and 'ctime'. The later represents the last change time for the inode. One can limit changes just to the 'mtime' or 'atime' by adding the corresponding -m or -a switch to touch. In the later cases the ctime is unconditionally altered --- another trace of potentially useful information.

1

You could use 'touch' for a number of reasons:

You can write a script that "encodes" some kind of program state in the existence or non-existence of a file and the touch command is a very accessible way to create a zero-length file.

Also, a program can use these time stamps to record the last time an event happened, so if you wanted to track the last time the event occurred, but it doesn't require changing the content of the file, then touch is an easy way to do it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .