mtime, ctime, and atime | Unix Linux Forums | Tips and Tutorials

  Go Back    


Tips and Tutorials Helpful articles from our Users.

mtime, ctime, and atime

Tips and Tutorials


Tags
atime, ctime, modification time, mtime, perl, perl shift, shift, shift perl, time

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-31-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 28 October 2014, 3:35 PM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 437 Times in 261 Posts
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:

Code:
$ 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.
The Following 17 Users Say Thank You to Perderabo For This Useful Post:
chapeupreto (11-27-2010), clx (03-30-2011), lramsb4u (05-16-2011), malcomex999 (02-15-2011), mde (03-12-2013), migurus (12-10-2013), mirni (07-30-2011), Neo (02-01-2014), PolywogSys (03-18-2011), pradeep.aradhya (01-02-2013), quadball (09-07-2012), raghukreddy.ab (01-11-2012), saltie (09-21-2012), srinath52069 (11-15-2011), ttening (01-17-2013), zioalex (02-18-2013), zzal_cn (09-13-2013)
Sponsored Links
    #2  
Old 12-28-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 28 October 2014, 3:35 PM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 437 Times in 261 Posts
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:
Code:
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..
The Following 8 Users Say Thank You to Perderabo For This Useful Post:
chapeupreto (11-27-2010), darthtina (09-20-2010), dips_ag (07-01-2011), lramsb4u (05-16-2011), Neo (02-01-2014), PolywogSys (03-18-2011), quadball (09-07-2012), raghukreddy.ab (01-11-2012)
Sponsored Links
    #3  
Old 08-30-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 28 October 2014, 3:35 PM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 437 Times in 261 Posts
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.



Code:
$ 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
hello
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
20070830083330
$ 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
20070830083157
$ 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
20070830083248
$

The Following 2 Users Say Thank You to Perderabo For This Useful Post:
posix (3 Weeks Ago), rwuerth (04-28-2011)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Why updating atime doesn't update ctime? password636 UNIX for Advanced & Expert Users 2 12-05-2011 06:35 AM
mtime VS atime VS ctime pinoy43v3r UNIX for Advanced & Expert Users 4 05-25-2010 12:03 PM
how to find ot ctime , mtime ,atime nilesrex Shell Programming and Scripting 5 11-08-2008 03:35 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



All times are GMT -4. The time now is 12:40 PM.