17

Can I change creation date of some file using Python in Linux?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Max Frai
  • 61,946
  • 78
  • 197
  • 306

5 Answers5

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)

Understanding UNIX / Linux file systems

Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
  • 1
    Note: there is `st_birthtime` on some Unix systems such as FreeBSD. – jfs Dec 23 '15 at 18:52
  • 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
  • 3
    It'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
  • 111,714
  • 37
  • 173
  • 152
  • 2
    >Set the access and modified times But i need creating date. – Max Frai May 20 '09 at 12:16
  • 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
  • 3
    It 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

Check out os.utime

os.utime(file_path,(new_atime,new_mtime))
Nazik
  • 8,696
  • 27
  • 77
  • 123
dahoiv
  • 85
  • 1
  • 5
  • 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
  • 29,931
  • 6
  • 88
  • 75
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
  • 42,588
  • 16
  • 104
  • 136