touch – change file timestamps
touch command is one of these little but extremely useful tools in Unix and Linux which you may have used for quite sometime before realizing their full potential. In short, it updates file timestamps – access and modification ones (atime and mtime respectively).
Why modify file timestamps?
There are quite a few legitimate reasons why you may want to update timestamps on a certain file. Ranging from source control approaches to storage usage analysis, there are processes out there which rely on the timestamps associated with each file and directory of yours.
After all, it’s always useful to know when the file was last modified or when somebody tried to access its contents.
Changing timestamps of a time to the current system time
The default behavior of touch command is to change all three timestamps associated with a file to the current system time.
You simply specify the filename as a command line parameter, no oother options are needed. If there isn’t a file with the specified name, touch command will create it for you if permissions allow it:
As you can see from the example, the file which isn’t originally found, gets created by the touch command and gets its timestamps set to the current system time and date.
Changing file timestamps to a specific date and time
If you have a specific time and date you would like to be used for all the timestamps of a file or directory, touch command will gladly accempt a timestamp template with -t command line option.
Template for the timestamp is quite thorough: [[CC]YY]MMDDhhmm[.ss], but it’s entirely up to you whether to specify the year (either two-digit or a full form) or not.
This example resets the date to October 16th:
As you can see from the output, both access time and modification time got updated. The reason change time (ctime) is set to a different date is because this field reflects the last update to the inode behind a file, and always reflects the current time. In other words, it’s set to Nov 18th 2008 because of the date of writing this example.
If you fancy adding a year to the timestamp specification, you can specify something from both past and future.
Here’s how easy it is to set atime and mtime to the Oct 16th, 2010 date:
Modifying atime of a file in Unix
Similar to the commands above, you can use -a option to make touch only update the access time field of a file:
Modifying mtime of a file in Unix
If you’re interested in updating the modification date only, use -m option:
Using a reference file to set atime and mtime
Finally, the really useful option for synchronizing access and modification time fields between multiple files is to use reference file. A reference file is the file which already has the timestamps you’d like to copy:
By specifying this file using a -r option, you can use the touch command to set the same atime and mtime values to any file of yours:
See Also
touch command is one of these little but extremely useful tools in Unix and Linux which you may have used for quite sometime before realizing their full potential. In short, it updates file timestamps – access and modification ones (atime and mtime respectively).
Why modify file timestamps?
There are quite a few legitimate reasons why you may want to update timestamps on a certain file. Ranging from source control approaches to storage usage analysis, there are processes out there which rely on the timestamps associated with each file and directory of yours.
After all, it’s always useful to know when the file was last modified or when somebody tried to access its contents.
Changing timestamps of a time to the current system time
The default behavior of touch command is to change all three timestamps associated with a file to the current system time.
You simply specify the filename as a command line parameter, no oother options are needed. If there isn’t a file with the specified name, touch command will create it for you if permissions allow it:
As you can see from the example, the file which isn’t originally found, gets created by the touch command and gets its timestamps set to the current system time and date.
Changing file timestamps to a specific date and time
If you have a specific time and date you would like to be used for all the timestamps of a file or directory, touch command will gladly accempt a timestamp template with -t command line option.
Template for the timestamp is quite thorough: [[CC]YY]MMDDhhmm[.ss], but it’s entirely up to you whether to specify the year (either two-digit or a full form) or not.
This example resets the date to October 16th:
As you can see from the output, both access time and modification time got updated. The reason change time (ctime) is set to a different date is because this field reflects the last update to the inode behind a file, and always reflects the current time. In other words, it’s set to Nov 18th 2008 because of the date of writing this example.
If you fancy adding a year to the timestamp specification, you can specify something from both past and future.
Here’s how easy it is to set atime and mtime to the Oct 16th, 2010 date:
Modifying atime of a file in Unix
Similar to the commands above, you can use -a option to make touch only update the access time field of a file:
Modifying mtime of a file in Unix
If you’re interested in updating the modification date only, use -m option:
Using a reference file to set atime and mtime
Finally, the really useful option for synchronizing access and modification time fields between multiple files is to use reference file. A reference file is the file which already has the timestamps you’d like to copy:
By specifying this file using a -r option, you can use the touch command to set the same atime and mtime values to any file of yours: