mtime, ctime, and atime

Login or Register to Reply

Thread Tools Search this Thread
# 1  
Old 07-31-2005
mtime, ctime, and atime

Unix keeps 3 timestamps for each file: mtime, ctime, and atime. Most people seem to understand atime (access time), it is when the file was last read. There does seem to be some confusion between mtime and ctime though. ctime is the inode change time while mtime is the file modification time. "Change" and "modification" are pretty much synonymous. There is no clue to be had by pondering those words. Instead you need to focus on what is being changed. mtime changes when you write to the file. It is the age of the data in the file. Whenever mtime changes, so does ctime. But ctime changes a few extra times. For example, it will change if you change the owner or the permissions on the file.

Let's look at a concrete example. We run a package called Samba that lets PC's access files. To change the Samba configuration, I just edit a file called smb.conf. (This changes mtime and ctime.) I don't need to take any other action to tell Samba that I changed that file. Every now and then Samba looks at the mtime on the file. If the mtime has changed, Samba rereads the file. Later that night our backup system runs. It uses ctime, which also changed so it backs up the file. But let's say that a couple of days later I notice that the permissions on smb.conf are 666. That's not good..anyone can edit the file. So I do a "chmod 644 smb.conf". This changes only ctime. Samba will not reread the file. But later that night, our backup program notices that ctime has changes, so it backs up the file. That way, if we lose the system and need to reload our backups, we get the new improved permission setting.

Here is a second example. Let's say that you have a data file called employees.txt which is a list of employees. And you have a program to print it out. The program not only prints the data, but it obtains the mtime and prints that too. Now someone has requested an employee list from the end of the year 2000 and you found a backup tape that has that file. Many restore programs will restore the mtime as well. When you run that program it will print an mtime from the end of the year 2000. But the ctime is today. So again, our backup program will see the file as needing to be backed up.

Suppose your restore program did not restore the mtime. You don't want your program to print today's date. Well no problem. mtime is under your control. You can set it to what ever you want. So just do:
$ touch -t 200012311800 employees.txt

This will set mtime back to the date you want and it sets ctime to now. You have complete control over mtime, but the system stays in control of ctime. So mtime is a little bit like the date on a letter while ctime is like the postmark on the envelope.
These 22 Users Gave Thanks to Perderabo For This Post:
chapeupreto (11-27-2010) clx (03-30-2011) lramsb4u (05-16-2011) malcomex999 (02-15-2011) mathbalaji (02-19-2016) mde (03-12-2013) migurus (12-10-2013) mirni (07-30-2011) mj0lnr (07-30-2015) Neo (02-01-2014) paxmercuria (08-11-2015) pradeep.aradhya (01-02-2013) quadball (09-07-2012) raghukreddy.ab (01-11-2012) saltie (09-21-2012) srinath52069 (11-15-2011) System Admin 77 (05-25-2016) ttening (01-17-2013) zioalex (02-18-2013) zzal_cn (09-13-2013)
# 2  
Old 12-28-2006
find command -mtime -ctime -atime

The find command uses arguments like:
-mtime -2
-mtime +2
-mtime 2

There are -ctime and -atime options as well. Since we now understand the differences among mtime, ctime, and atime, by understanding how find uses the -mtime option, the other two become understood as well. So I will describe find's use of the -mtime option.

As you probably know, the find command can run for minutes or hours depending on the size of the filesystem being searched. The find command makes a note of its own start time. It then looks at a file's mtime and computes how many seconds ago the file was modified. By dividing the seconds by 86,400 (and discarding any remainder), it can calculate the file's age in days:
0 days in seconds:       0  -   86399
1 day in seconds:    86400  -  172799
2 days in seconds:  172800  -  259159

So now that we know how many days ago a file was modified, we can use stuff like "-mtime 2" which specifies files that are 172800 to 259159 seconds older than the instant that the find command was started.

"-mtime -2" means files that are less than 2 days old, such as a file that is 0 or 1 days old.

"-mtime +2" means files that are more than 2 days old... {3, 4, 5, ...}

It may seem odd, but +0 is supposed to work and would mean files more than 0 days old. It is very important to recognize that find's concept of a "day" has nothing to do with midnight.

Last edited by Perderabo; 08-05-2007 at 11:40 AM..
These 11 Users Gave Thanks to Perderabo For This Post:
chapeupreto (11-27-2010) darthtina (09-20-2010) dips_ag (07-01-2011) lramsb4u (05-16-2011) mathbalaji (02-19-2016) Neo (02-01-2014) quadball (09-07-2012) raghukreddy.ab (01-11-2012) System Admin 77 (05-25-2016)
# 3  
Old 08-30-2007
Using perl to display the file timestamps

The ls program will display mtime if you use "ls -l". And you can get atime or ctime with "ls -lu" or "ls -lc". But ls uses a strange format. It displays the month and day in all cases. If the timestamp is recent, it also displays hour and minute. If the timestamp is older than 6 months, it display the year instead of hour and minute. A clever script can reformat this to year, month, day, hour, and minute. But ls will not display the seconds. The gnu version of ls (which is usually the only version on linux) does have extended options like --fulltime. But these extended options are non-standard and won't be available on other versions of Unix.

The perl language is also non-standard, but perl tends to be available on most versions of unix. For example, a version of perl is supplied with HP-UX and Solaris. Perl can easily display the timestamps of files. Here are some perl one-liners to display atime, mtime, and ctime.

$ echo hello > testfile ; date
Thu Aug 30 08:31:57 EDT 2007
$ chmod 700 testfile ; date
Thu Aug 30 08:32:48 EDT 2007
$ cat testfile ; date
Thu Aug 30 08:33:30 EDT 2007
$ perl -e '@d=localtime ((stat(shift))[8]); printf "%4d%02d%02d%02d%02d%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]' testfile
$ perl -e '@d=localtime ((stat(shift))[9]); printf "%4d%02d%02d%02d%02d%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]' testfile
$ perl -e '@d=localtime ((stat(shift))[10]); printf "%4d%02d%02d%02d%02d%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]' testfile

These 4 Users Gave Thanks to Perderabo For This Post:
mad man (08-24-2016) posix (10-07-2014) rwuerth (04-28-2011)
Login or Register to Reply

Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Zfs - what does atime update? jabberwocky Solaris 5 09-10-2015 09:19 AM
Ls -l displays ctime or mtime? rupeshkp728 UNIX for Dummies Questions & Answers 10 05-07-2013 11:12 AM
Having a hell of atime with KSH -ftp automation Astrocloud UNIX for Dummies Questions & Answers 3 05-29-2012 05:37 PM
find -ctime BeefStu Shell Programming and Scripting 5 01-23-2012 07:10 PM
Why updating atime doesn't update ctime? password636 UNIX for Advanced & Expert Users 2 12-05-2011 06:35 AM
-atime not working as expected canon273 UNIX for Dummies Questions & Answers 10 09-30-2011 02:56 PM
mtime VS atime VS ctime pinoy43v3r UNIX for Advanced & Expert Users 4 05-25-2010 12:03 PM
(find) mtime vs. (unix) mtime hiddenshadow UNIX for Dummies Questions & Answers 2 12-15-2009 08:25 AM
how to find ot ctime , mtime ,atime nilesrex Shell Programming and Scripting 5 11-08-2008 03:35 PM
read() without changing atime ? tantric Programming 8 04-08-2007 10:43 PM
atime, ctime, mtime somewhere along csize.. moxxx68 UNIX for Dummies Questions & Answers 4 03-02-2005 05:14 PM
mtime vs ctime moxxx68 UNIX for Dummies Questions & Answers 3 11-06-2004 09:57 PM