Can I change creation date of some file using Python in Linux?
Asked
Active
Viewed 2.4k times
5 Answers
18
Linux and Unix file system stores :
File access, change and modification time (remember UNIX or Linux never stores file creation time, this is favorite question asked in UNIX/Linux sys admin job interview)
Ólafur Waage
- 66,813
- 19
- 139
- 194
-
-
you can extract crtime from ext4 fs using [xstat function](http://moiseevigor.github.io/software/2015/01/30/get-file-creation-time-on-linux-with-ext4/) – jfs Nov 08 '17 at 12:24
-
2It's no longer true that Linux no longer supports creation time. See my answer below. – Gringo Suave Feb 14 '19 at 02:06
-
The thing is, while ext4 do [support](https://en.wikipedia.org/wiki/Ext4#Features) this *crtime* attribute, [AFAIU](https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-preserve-crtime-creation-birth-time-when-copying-from-windows-ntfs-to-linux-ext4-4175625229/#post5829217) that's not supposed to hold the same meaning than in other (or just Windows?) file systems. It's a specific property of the inode, not the contents itself. And I think for this reason there isn't any API to change or set it (which was actually what OP had asked). – mirh Feb 06 '20 at 15:37
16
You can use os.utime to change access and modify time but not the creation date.
Community
- 1
- 1
Nadia Alramli
- 105,894
- 35
- 170
- 151
-
2
-
Quote from the docs on that function: "Set the access and modified times of the file specified by path." .. so you can't use it to set the creation date. – Epcylon May 20 '09 at 12:20
-
3It would be nice to add that there the concept of file creation date does not exist in most native *nix filesystems. – tzot May 21 '09 at 08:58
2
-
it doesn't change *creation* time as [@Nadia Alramli's answer explicitly says](http://stackoverflow.com/a/887564/4279). – jfs Dec 23 '15 at 18:53
2
It's no longer true that Linux doesn't support creation time. See:
Note this specific answer to view C-code that displays the field:
If the author of that post can help I might be able to create a wrapper with ctypes to modify it from Python.
Gringo Suave
- 27,899
- 6
- 81
- 73
2
I am not a UNIX expert, so maybe I'm wrong, but I think that UNIX (or Linux) don't store file creation time.
Nick Dandoulakis
- 41,532
- 15
- 102
- 136