Sponsors

How to Modify the timestamp (Date and Time) of a File on Linux

There are certain circumstances where you may want to modify the date and time of a file on Linux. In my case I was testing a cronjob that moved and deleted files older than a certain date and testing it was much easier after modifying the date of the files.

The command to use for modifying the date and time of a file is called touch. To modify the timestamp of a file issue one of the below commands. The file used in each of the examples below is named testfile.

Modify Date & Time of a File:

[root@dev ~]# touch -d "Sep 16 11:11" testfile

The above will modify the Date and time of the file “testfile” to the current Sep 16th of the current year with a time of 11:11 AM.

Modify Date Including the Year of a File:

[root@idev touch]# touch -d "Sep 16 2007" testfile

The above will modify the date and year of the file testfile to Sep 16th of the year 2007.

Please note that the -d switch of the command touch will accept various different date structures so there are many different ways to actually specify the date you are modifying. I know the above to examples work on CentOS however there are more than likely numerous other ways to specify the date with the -d switch for the touch command.