Create/modify a UNIX file with an arbitrary timestamp

Contributor Icon Contributed by qmchenry Date Icon April 9, 2004  
Tag Icon Tagged: Solaris system administration

The touch command in UNIX creates a file if it doesn’t exist or updates the modification time of an existing file to the current time. An option of the touch command allows the modification timestamp to be set to any arbitrary time.


To change the modification time of a file (the time displayed in a long listing of the file) of a file called testfile to November 18, 2000, 2:30 PM, use the following command:

touch -t 200011181430

This will alter the modification time of an existing testfile or, if not present, will create an empty file with that timestamp.

The format of the timestamp is
[[CC]YY]MMDDhhmm [.SS]

Date and time elements in square brackets are optional, so the minimum timestamp includes month, day, hour, and minute.

Previous recipe | Next recipe |
 
  • gandalf
    How do you handle symbolic links? The touch command affects the target of the symbolic link and not the link itself.
  • qmchenry
    Good question. I've never noticed this behavior before. I don't see any options for touch that would make it not follow the link. If you really need the link to be updated by a touch and it is in the same filesystem as the real file, you could use a hard link. When you touch a hard link, it updates both the link and the original file. The hard link doesn't look like a symbolic link, it just looks like a regular file, which may not be desirable. That's the only suggestion that I have. Anyone else?

    Quinn
  • Migxi
    Excellent..... now.....

    Who to stamp the timeref file with a date like 15 days ago ?
  • fracai
    touch -B <#> will set the timestamp to a date in the past by the number of seconds provided

    ie. touch -B 10 will make the file appear 10 seconds older than the current date.

    man touch is more helpful than waiting a few months for an answer :)
blog comments powered by Disqus