Shell Script Nuggets: File Date Manipulation And Checking
This one goes out to all the shell script junkies out there as well as those who are just starting to pick up the dark art. Often times, you’ll find yourself needing to manipulate and check access, creation, and manipulation dates of different files. The reason for doing this may range from the need to delete files based on dates, check for newer files, or simply because you have nothing better to do.
For these odd occasions, two Unix utilities come in handy; touch and find.
Before going any further, it is important to realise that in both Unix and Windows operating systems, there are three different times that are related to each file as follows:
- Creation time (ctime): The time when a file was first created
- Modification time (mtime): The time when the file was last changed
- Access time (atime): The time when the file was last opened
touch
‘touch’ is useful for when you need to update the modification or access date of a particular file. Once again, reasons for using this utility may vary. Probably the most common use is when you want to con someone into believing that you completed your work earlier than you actually did. For that purpose, changing the file’s modification time is the way to go. You can do this with the following command:
$ touch -t time file
Where time is in the form YYMMDDHHmm.
For example the following would change the modification time of a file to midnight 28th June 2006:
$ touch -t 200606280000 file
Depending on which time you need to change, you can specify the -a flag for ‘access time’ and the -m flag changes the modification time. The creation time cannot be changed.
find
‘find’ is a simple tool for finding and listing contents within the file system. You might argue that ‘ls’ does the very same thing. What makes ‘find’ more useful than ‘ls’ is the ability to provide it with certain conditions that need to be met, several of which deal with dates and time. The time manipulation flags are as follows:
- -ctime
- -mtime
- -atime
- -newer
-ctime, -mtime, and -atime all have the same form ‘[flag] n’, where n is a numeric value. The flag evaluates true if the specified corresponding date of a file is within n-1 days of the current time. For example, to find files that were modified 24 hours before the current time, you may use the following command:
$ find . -mtime 1
Note that the three flags above work in 24 hour increments from the current date. You can’t specify a specific date as a cutoff point. In other words, if you issued the command at noon, files created within the last 24 hours (up to noon the day before) will be found and listed.
-newer comes in the following form:
-newer[tv1[tv2]] file
Where tv1 and tv2 are optional extensions that can be used to compare specific time values (e.g. creation, modification and access). Without the modifiers, the modification time is compared by default. The times of each file within the search path is compared with the time of the file that is passed as an argument of the flag. This is useful for finding files that are newer (or older) than a specific file that you have on hand.
Putting them together
With these two utilities, you can do quite a bit of funky magic. One particularly useful combination is when you wish to find files that were created after a certain date (e.g. 26 June 2006). This might be useful if you need to archive certain files, or when you need to detect and do something special when a file is newly modified, or perhaps you want to make sure that no one’s accessed your porn collection since you last seen it. To do so, the following combination can be used:
$ touch -t200606260000 reffile
$ find . -newer reffile # Checks for mod time
$ find . -newerm reffile # Alternative check for mod time
$ find . -newera reffile # Checks for access time
$ find . -newerc reffile # Checks for creation time
To find files older than the reffile, simply invert the search with the ‘!’ modifier:
$ find . ! -newer reffile # Finds files that were modified before reffile
Don’t worry if the file ‘reffile’ does not exist. Touch by default will create the file if it can’t be found (you can switch this off with the -c argument). Just two simple lines to search for files newer than a certain date. I’m sure you can dream up a hundred different uses for these functions in combination with other things. I’ll leave it to your imagination.
For full specifications of the Unix functions used above, read the manual. There are plenty of other uses for the functions and I’m sure you’ll come across existing, and perhaps even think up new, ones sooner or later. Have something useful to share? Feel free to leave a comment.
Related posts:
Shell Script Nuggets: Simulating File Locks
Perl Scripting: Checking CPAN Module Versions
More Friday The 13th
Shell Script Nuggets: Mathematic Computations
Tech: Rsync rulez!

WL Said,
June 28, 2006 @ 9:35 pm
Windows don’t have similar utilities?
Simonsays Said,
June 29, 2006 @ 4:24 pm
WL: I guess for Windows one could look at the Windows XP command-line reference, but I doubt that an equivalent of
touchis there. I like supplementing the bare-bones NT command line with Cygwin because all the Linux bash coreutils commands are accessible after adding the /bin directory to the User Account environment variables.Hmmm, for those of you who don’t know what we’re talking about… download Cygwin/Knopppix Live CD and give it a go!
gbyeow Said,
June 30, 2006 @ 11:48 am
It is not part of the default Windows utilities. Do what Simon-says’.
Stoney Said,
May 29, 2007 @ 9:56 pm
ctime is not really the creation time of the file. It is the creation time of the inode of the file or when the inode was last changed.
But otherwise a good post.
-S