Changing the date of a file under Linux or Mac OS X can be quite a challenge. As far as I know both do not provide a UI (User Interface) option to do this out of the box, but what both operating systems do provide are the tools needed to do it in the console.
As with most console based operations they can be a little bit more tricky then just clicking a few buttons on the UI. Surprisingly the command line tool called ‘touch’ is the program we need to manipulate the date of a file or directory. If you know the touch command at all, you have probably used it for creating files with no content.
Understanding the date attributes
Each file on the filesystem has a number of attributes and some of these are date related. Linux based operating systems provide 3 different date stamps for a file, though their availability depends on the file system and file system options. It is common practice, for example, to disable the access time on SSD discs to minimize write access to the SSD. Assuming they are all present, the three date attributes are:
Modification: The date/time when the file content was last modified. This attribute is often referred to as mtime.
Change: The date/time when the file’s attributes like owner, permission, etc, were last changed. This attribute is often referred to as ctime.
Access: The date/time when the file was last accessed. This attribute is updated when the file is read. To optimize the number of write operations this is often disabled on SSD discs. This attribute is often referred to as atime.
To see all this date information for a file use the stat command. (There is a small difference when you use this command on Mac OS X. To get the same output on Mac OS X add the parameter -x to the command.)
stat filename.ext
The command should show you – along with some other information – the date attributes we are interested in. Depending on the operating system and date format settings the output might present the date in a different format.
Access: 2013-04-09 13:53:19.000000000 +0000 Modify: 2013-03-25 13:33:48.000000000 +0000 Change: 2013-03-22 13:33:44.000000000 +0000
Changing the date of a file
As mentioned earlier, the touch program will help us to change the date of a file. The most common use case for the touch command is creating an empty file. To do that you would call:
touch filename.txt
That would create the file ‘filename.txt’ without any content, but as we want to change the date of a file we have to pass a couple more arguments to the program. If you check the man page, you will find a number of arguments to the touch program. I will focus here on the arguments related to manipulating the date/time of files.
The touch command provides the following possibilities.
-at [[CC]YY]MMDDhhmm[.ss]
This changes the access date of the file to the provided time. The commandline would therefore look like this: “touch -at 20130410120530 filename.txt” to change the date to April 10. 2013 12:05:30.
-mt [[CC]YY]MMDDhhmm[.ss]
This changes the modification date of the file to the provided time. The commandline would therefore look like this: “touch -mt 20130410120530 filename.txt” to change the date to April 10. 2013 12:05:30.
If you called “touch -t 20130410120530 filename.txt” it will change both the modification and the access date.
-A [[hh]mm]SS
The Mac OS X version of touch comes with another interesting option. The -A parameter modifies the date relative to the file’s date. -A 99 means 99 seconds will be added to the files modification and change date. When the number is prefixed with a “-” minus, the amount is subsracted from the files date. The maximum change therefore is 99 hours 99 minutes and 99 seconds (This works out to a change of 100 hours 40 minutes 39 seconds).
I needed the date to be changed on a directory and all of its sub-directories and files. So i created this small script to do it:
#!/bin/bash DIRECTORIES=`find . -type d` for D in $DIRECTORIES do touch -A 999999 $D/* #touch -t $1 $D/* done
To change another date, or to use the script under Linux, just use the commented-out touch command in the script. It uses the first parameter to the script to define the date in the format shown above.
Read more of my posts on my blog at http://blog.tinned-software.net/.