Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 851
misguided Avatar asked Jul 30 '13 23:07

misguided


1 Answers

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.)

like image 181
eaj Avatar answered Oct 02 '22 00:10

eaj