Sometimes it is necessary to change a file or even a directory’s timestamp. This sounds easy, but most GUI utilities installed out of the box on OS X as well as on Linux don’t let you change a file’s timestamp. Besides utilities specially crafted for making date/time changes, Linux and OS X both come with a utility to change the date/time from the command line.
Challenged with the task to change a directory’s date or time stamp, the first thing to do is check what date information is already assigned to the directory. With the help of the stat(1) utility, the file or directory state can be checked.
$ stat testdir File: `testdir' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 22h/34d Inode: 8782228 Links: 2 Access: (0755/drwxr-xr-x) Uid: ( 500/username) Gid: ( 100/ users) Access: 2016-03-14 09:24:06.325213266 +0000 Modify: 2016-03-14 09:24:06.325213266 +0000 Change: 2016-03-14 09:24:06.325213266 +0000
In OS X the “stat” utility shows slightly different output when called without options. To get the same result as under Linux, the -x option needs to be added to the command. This is because of the different origin of the stat utility. Most Linux distributions provide the GNU stat while OS X provides the BSD version.
$ stat -x testdir File: "testdir" Size: 68 FileType: Directory Mode: (0755/drwxr-xr-x) Uid: ( 501/ username) Gid: ( 20/ staff) Device: 1,6 Inode: 65239844 Links: 2 Access: Tue Mar 15 00:15:43 2016 Modify: Tue Mar 15 00:15:43 2016 Change: Tue Mar 15 00:15:43 2016
In the file listing it shows the date (like in the above output from stat) but will less details for the timestamp.
$ ll total 0 drwxr-xr-x 2 username users 4096 Mar 14 09:24 testdir -rw-r--r-- 1 username users 0 Mar 14 09:24 testfile
To change the date/time stamp of this directory, one of the below commands using touch(1) can be used. The only difference between the “–date” and the “-t” option is the format in which the date is provided. The “–date” version uses a more readable representation of the date, while the format of the “-t” option is “[YYYY]MMDDhhmm[.ss]”.
$ touch --date="2015-01-01 22:15:30" testdir $ touch -t 201501012215.30 testdir
Executing the stat utility again shows the changed timestamps for the Access and Modify timestamp. The output of the ll command also shows the changed timestamp.
$ stat testdir File: `testdir' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 22h/34d Inode: 8782228 Links: 2 Access: (0755/drwxr-xr-x) Uid: ( 500/username) Gid: ( 100/ users) Access: 2015-01-01 22:15:30.000000000 +0000 Modify: 2015-01-01 22:15:30.000000000 +0000 Change: 2016-03-14 17:57:08.185884619 +0000 $ ll total 4 drwxr-xr-x 2 username users 4096 Jan 1 2015 testdir -rw-r--r-- 1 username users 0 Mar 14 09:24 testfile
In the same way as the date and time of the directory was changed it is possible to change these for files. The following shows the above examples repeated for a file.
$ stat testfile File: `testfile' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 22h/34d Inode: 8783014 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 500/username) Gid: ( 100/ users) Access: 2016-03-14 09:24:13.495275892 +0000 Modify: 2016-03-14 09:24:13.495275892 +0000 Change: 2016-03-14 18:03:25.912674045 +0000 $ ll total 4 drwxr-xr-x 2 username users 4096 Jan 1 2015 testdir -rw-r--r-- 1 username users 0 Mar 14 09:24 testfile $ touch --date="2015-06-22 20:10:05" testfile $ stat testfile File: `testfile' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 22h/34d Inode: 8783014 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 500/username) Gid: ( 100/ users) Access: 2015-06-22 20:10:05.000000000 +0000 Modify: 2015-06-22 20:10:05.000000000 +0000 Change: 2016-03-14 18:07:11.097919241 +0000 $ ll total 4 drwxr-xr-x 2 username users 4096 Jan 1 2015 testdir -rw-r--r-- 1 username users 0 Jun 22 2015 testfile
With the “touch” utility you can even control if the modify date or the access date only should be changed. To find out how, please refer to the touch(1) man page.
Read more of my posts on my blog at http://blog.tinned-software.net/.