Difference between python – getmtime() and getctime() in unix system

Question:

Can someone please specify what is the difference between os.path.getmtime(path) and os.path.getctime(path) in unix systems . As per the defnition in python docs:

os.path.getmtime(path)

Return the time of last modification of path. The return value is a
number giving the number of seconds since the epoch (see the time
module). Raise os.error if the file does not exist or is inaccessible.

os.path.getctime(path)

Return the system’s ctime which, on some systems (like Unix) is the
time of the last change, and, on others (like Windows), is the
creation time for path. The return value is a number giving the number
of seconds since the epoch (see the time module). Raise os.error if
the file does not exist or is inaccessible.

Does that basically mean they are the same things when used in unix/systems?

#!/usr/bin/python
import os
print os.path.getmtime('File')
print os.path.getctime('FIle')

Both the prints fetch me the same value.

I am basically looking for last creation date for file , rather than last modification date. Is there a way to achieve the same in unix?

Asked By: misguided

||

Answers:

This is technically not a programming question and therefore shouldn’t be on Stack Overflow, but you can find the answers you seek here—which happens to be the first Google result for ctime mtime atime. Short answer: ctime changes when the file’s ownership or permissions change, as well as when the data in the file changes. mtime changes only when the data in the file changes.

Answered By: kindall

From the man page on stat, which os.path.getmtime() and os.path.getctime() both use on Unix systems:

The field st_mtime is changed by file modifications, for example, by mknod(2), truncate(2), utime(2) and write(2) (of more than zero bytes). Moreover, st_mtime of a directory is changed by the creation or deletion of files in that directory. The st_mtime field is not changed for changes in owner, group, hard link count, or mode.

The field st_ctime is changed by writing or by setting inode information (i.e., owner, group, link count, mode, etc.).

So no, these are not the same.

Answered By: Andrew Clark

The mtime refers to last time the file’s contents were changed. This can be altered on unix systems in various ways. Often, when you restore files from backup, the mtime is altered to indicate the last time the contents were changed before the backup was made.

The ctime indicates the last time the inode was altered. This cannot be changed. In the above example with the backup, the ctime will still reflect the time of file restoration. Additionally, ctime is updated when things like file permissions are changed.

Unfortunately, there’s usually no way to find the original date of file creation. This is a limitation of the underlying filesystem. I believe the ext4 filesystem has added creation date to the inode, and Apple’s HFS also supports it, but I’m not sure how you’d go about retrieving it in Python. (The C stat function and the corresponding stat command should show you that information on filesystems that support it.)

Answered By: eaj
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.